mercredi 28 octobre 2015

AngularJS: Binding Checkbox to Object in MongoDB Collection

I am still fairly new to NodeJS, MongoDB and AngularJS.

I have a collection called 'Strategy' where each document defines a trading Strategy involving multiple bitcoin exchanges.

I've got the following section of schema defined for my 'Strategy' collection:

    ...
    primaryExchanges: [{
        exchange: {
            type: Schema.ObjectId,
            ref: 'Exchange'
        }, 
        ratio: {
            type: Number,
            default: 0,
        }
    }],
    ...
    totalCoins: {
        type: Number,
        default: 100,
    },
    ...

exchange: a reference to an object in the Exchanges collection, ratio: how much of the strategy's totalCoins should be spent on that exchange.

Each exchange has a seperate set of 'instruments' that they support trading on, and I'd like to represent which instruments the user wants this particular strategy to trade on.

Ideally, I'd like get a document back for the 'Strategy' collection that looks something like this:

    totalCoins: 100,
    primaryExchanges: [
        {
            exchange: idForExchange1,
            ratio: 0.6,
            instruments: [
                {name: "instrument a", enabled: true},
                {name: "instrument b", enabled: false}
            ]
        },
        {
            exchange: idForExchange2,
            ratio: 0.4,
            instruments: [
                {name: "instrument c", enabled: true},
                {name: "instrument d", enabled: true},
                ...
            ]
        }
    ]

So I modified my schema to look like the following:

    ...
    primaryExchanges: [{
        exchange: {
            type: Schema.ObjectId,
            ref: 'Exchange'
        }, 
        ratio: {
            type: Number,
            default: 0,
        },
        instruments: [{
            name: {
                type: String,
                default: ''
            },
            enabled: {
                type: Boolean,
                default: false
            }
        }]
    }],
    ...

In my view, I would like to be able to display which instruments on the exchange are enabled/disabled for trading using a set of checkboxes.

    Exchange1:
        Ratio: 0.6
        Use Instruments:
            [x] instrument a    [] instrument b

    Exchange2:
        Ratio: 0.4
        Use Instruments:
            [x] instrument c    [x] instrument d

When I go to save the model using my edit view, the document in the database isn't get updated, I believe it's the way I am using data-ng-model to bind to the instruments.

In my view I have an ng-repeat to iterate over each of the primaryExchange elements, I am using something like this to bind to the checkboxes:

    <div class="columns col-lg-3" data-ng-repeat="instrument in exchange.instruments">
        <label><input data-ng-model="strategy.primaryExchanges[$index].instruments[instrument]" type="checkbox" value="{{instrument}}">{{instrument}}</label>
    </div>

In this view, each "instrument" in "exchange.instruments" is simply a string with the instrument's name:

    // exchange.instruments
    ['instrument a', 'instrument b']

How could I modify my data-ng-model directives so that each checkbox is bound to an instrument belonging to the current primaryExchange in my list?

Thanks




Aucun commentaire:

Enregistrer un commentaire