How can I access to some model in Backbone collection - backbone.js

I have a collection of models:
city = new M.City
App.citiesList = new C.CitiesList model: city
App.citiesList.fetch()
How can I access to the model with id=15 for example?
I need something like App.citiesList.find(15).name(), where name() is model function
When I try to add function find to the collection it is incorrect.
When I try to iterate over App.citiesList.models - I see only one model or what it is.. I actually don't know.
Thanks a lot!

If App.citiesList is a Backbone collection then you'd want to use get:
get collection.get(id)
Get a model from a collection, specified by id.
So this would get you your model from the collection:
fifteen = App.citiesList.get 15
And if you wanted to call a method on it:
App.citiesList.get(15).name()
You'd probably want to make sure you got something back from App.citiesList.get 15 first though (unless you knew it was there of course). Since you're working in CoffeeScript you could use the existence operator like this:
name = App.citiesList.get(15)?.name()
#----------------------------^
to get 15's name or undefined in the name variable.
The find method on App.citiesList would be Underscore's find and that doesn't find an object with a particular ID.

Related

What is the difference between a model object queried by filter and an object queried by get() in Django?

I keep coming across this issue where I am trying to update a record using the update() method.
It always works when I query an object using filter.
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.filter(pk=1)
thing[0].update(**my_dictionary) wrote it wrong in the original question.
thing.update(**my_dictionary)
When I query the object using get() it keeps telling me that the object has no method update()
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.get(pk=1)
thing.update(**my_dictionary)
Isn't a model object the same in both cases? Why would one have an update method and the other one not? Any insight would be greatly appreciated.
The documentation is very explicit about this:
filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
If you know there is only one object that matches your query, you can use the get() method on a Manager which returns the object directly.
Your first snippet returns a QuerySet, which has an update method. The second snippet returns a model instance, which doesn't.
Note that you have not shown the exact code you are using: thing[0].update would give exactly the same error as the second snippet.
You're using QuerySet.update() and ModelInstance.save().
If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory. For example, instead of doing this:
e = Entry.objects.get(id=10)
e.comments_on = False
e.save()
...do this:
Entry.objects.filter(id=10).update(comments_on=False)

Backbone: pluck models with an array of IDs, and save them

So I have an array of IDs:
var myIDs = [1,5,9];
I have a collection that I want to search through, and pluck from. I thought I could do something like the following:
var searchResults = myCollection.where({"uID" : myIDs});
Of course that won't work, but there must be a way to achieve something similar.
Once I have the selected models, the plan is to edit the contents of, then save. Am I correct in assuming I can save the whole batch by doing the following?
myCollection.reset(searchResults);
I'm a total n00b to Backbone, obviously.
You can use Collection.filter to compare each item against the array:
var searchResults = myCollection.filter(function(model) {
return myIDs.indexOf(model.id) != -1;
});
("Where" is like a special case of "filter", with a specific iterator -- it compares the properties of each model with the hash set you provide.)
As far as saving, if you mean replacing the items in the collection, then yes, you can use reset for that. (Note that "save" in Backbone parlance normally means syncing model updates back to the server.)

setting hierarchical property for GXT's ComboBox.setDisplayField

i have a simple extension of a BaseModelData in a form of MyModel, and i can call new MyModel().getObj1().getObj2() to get to obj2's string value. i have a number of MyModel instances, so i would like to populate a ComboBox instance with an obj2 value from each MyModel instance. first, i called ComboBox.setDisplayField("obj1.obj2"), because using such hierarchical property approach works for TextField.setName() cases. then, i took a store which contains all MyModel instances, and set it to a ComboBox via setStore(). however, the combobox is empty. it looks as though setting the aforementioned property via ComboBox.setDisplayField() does not work the same way as it does for TextField.setName(). i tried using my own instance of ListModelPropertyEditor, but without success. so what are my alternatives?
thank you for your time!!!
I am not sure about accessing hierarchical data from ComboBox.setDisplayField() method, but can you can achieve it by adding a new method say getObj2() in MyModel class, which will essentially represent obj1.obj2.
public Obj2 getObj2() {
return getObj1().getObj2(); //with possible null checks
}
Now you can call ComboBox.setDisplayField("obj2") and get the work done.

replace collection in a view

I want to replace a collection inside a view. I use the reset command like this:
var maColl=mContent.get(ici).get("svgParameterList");
msvgParameterListView.collection.reset(maColl);
A JSON.stringify gives this :
maColl
[{"id":"x","name":"x"},....{"id":"style","name":"style"}]
msvgParameterListView.collection
[[{"id":"x","name":"x"},....{"id":"style","name":"style"}]]
Now, i have my collection in an array, so when i render it return null value.
How to change array of collection into collection ?
In short, how to make msvgParameterListView.collection equal to maColl ?
Note the [[ ]] for the new collection.
Note maColl is a collection inside another collection.
If I understand correctly, you are trying to reset a collection with the models of another collection? collection.toJSON is your friend:
toJSON collection.toJSON()
Return an array containing the attributes hash of each model in the collection. This can be used to
serialize and persist the collection as a whole. The name of this
method is a bit confusing, because it conforms to JavaScript's JSON
API.
which could be applied like this
msvgParameterListView.collection.reset(maColl.toJSON());
Or if you want to keep a reference to the original models, pass maColl.models
msvgParameterListView.collection.reset(maColl.models);
Passing directly a collection to reset will only confuse Backbone.

How to alter a backbone.js collection using equivalent of .slice(start, end)

My Backbone Collection receives 30 models on fetch().
I have tried
newColl=origColl.first(2);
to return a new Collection. When I try to pass to the View it won't work.
Is there anyway to do this using an underscore.js method?
Your code doesn't work likely because first(n) returns an array while your view is expecting a collection. You need to wrap it like this:
var newColl = new Backbone.Collection(origColl.first(2));
For a general "slice" you can use find in conjunction with _.range:
var newColl = new Backbone.Collection(origColl.find(_.range(start, end));
Naturally, tou can use your own collection class instead of Backbone.Collection.
See the documentation of range at http://documentcloud.github.com/underscore/#range.
I am not sure I understand your question correctly, but the following snippet might be what you are looking for.
var model = origColl.at(2);
var models = [model];
var newColl = new YourCollection(models);
newColl will now be a new collection containing the model at position 2 of your original collection. The example is a bit more verbose than I would write it, but it might make it clearer for you.

Resources