Question

usersCollection.fetch({
    success: function () {
        var getModel = usersCollection.where(checkIDJSON);
        //update that partcular attribute
        getModel.set('interest', 'rolling stones');
        console.log("Users:" + usersCollection.toJSON());

    },
    error: function () {
        // something is wrong..
    }

});

After running the code, it complains that the function is undefined, when trying to save to the model. Any idea why? thanks

I am using backbone.js in titanium mobile

Was it helpful?

Solution

From the fine manual:

where collection.where(attributes)

Return an array of all the models in a collection that match the passed attributes.

So when you say this:

var getModel = usersCollection.where(checkIDJSON);

you end up with an array of models in getModel. If you're sure that there will only be one model that matches, then use findWhere:

findWhere collection.findWhere(attributes)

Just like where, but directly returns only the first model in the collection that matches the passed attributes.

like this:

var getModel = usersCollection.findWhere(checkIDJSON);

If there could be multiple matches then presumably you'd want to call set on each one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top