As i made a collection of Users in backbone, I added few users in users collection and it is added.now i want to remove the a particular user from my collection with particular username.
So what i have done is i made a method in my collection:
user: function(username) {
return this.filter(function(user){ return user.get('username')==username; });
}
and then i access it like this:
App.Users.user(data.username);
then for removing a particular element i do:
App.Users.remove(App.Users.user(data.user));
and for updating the user data i do:
App.Users.user(data.username)[0].set(data)
Please check my code, and suggest if i can improve it any ways.
Instead of App.Users, use App.Models.Users. This will increase readability. Everything else seems fine to me.
If a username is unique, then you should use this.detect() instead of this.filter(). It would be slightly faster and also I believe also returns a single model instead of an array...
If the username isn't unique, ignore this...
Also, you should use '===' instead of '=='.
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.