简体   繁体   中英

backbone.js is not saving model with updated attributes

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM