Get model attributes in backbone.js

I have this model

var Item = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); var onSuccess = function(){ alert("success"); }; 

And a collection

 var Items = Backbone.Collection.extend({ model: Item }); 

And the rest of my code is here:

 var item = new Item(); var items = new Items(); item.fetch({ success: onSuccess }); alert(items.get("ItemCode")); 

I want to just get the attributes of the model. Now I have it on firebug. Also, when I run it in a browser, I get a success warning, and the next warning is undefined . enter image description here

This is the conclusion:

 {"ErrorMessage":null,"Items":[{"ErrorMessage":null,"CategoryCode":"Event Materials","ClassCode":null,"Components":null,"GroupCode":null,"ImageURL":null,"ItemCode":"ITEM-123","ItemDescription":"Old World Lamppost\u000d\u000a\u000d\u000a","ItemName":"GET123","ItemType":null,"KitItem":null,"Matrix":null,"Prefix":null,"RetailPrice":107.990000,"SalesTaxCode":null,"UPCCode":null,"UnitMeasureCode":"EACH","UnitsInStock":0,"Value":null,"WholesalePrice":95.000000}]} 

Note

This is just one of the elements that it returns. I just wrote on the subject so that it is not so long.

+6
source share
2 answers

You call get in your collection (see http://documentcloud.github.com/backbone/#Collection-get )

It seems that you really want to iterate over the collection and click on each item

 items.each(function(item) { item.get('ItemCode'); }); 

If not, please inquire!

In addition, if your model URL responds with a list of models, you must define the url attribute in your collection instead of your model.

 var Items = Backbone.Collection.extend({ model: Item, url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); 

And your answer should be an array with elements as elements of the array [<item1>, <item2>, ...] , and not a JSON object with {'Items': [<item1>, <item2>, ...] } . If you do not want to change the answer, you will need to implement the parse function in your collection ( http://documentcloud.github.com/backbone/#Collection-parse ).

Also, as @chiborg mentions, you call get right after fetch (which will execute asynchronously), so there is no guarantee that your data will be available.

The correct solution here is to link the 'refresh' listener in the collection.

 items.on('refresh', function() { // now you have access to the updated collection }, items); 
+10
source

This is due to the unsynchronized loading of the model - item.get("ItemCode") will work only after the model is loaded using fetch . Try calling it in your onSuccess function.

Also, note that this will not help to initialize Item directly. What you are trying to do is get the item in the Items collection, and then call item.get("ItemCode") on that item, for example:

 function onSuccess() { alert('success') var item = items.get(13); // get item with id 13 alert(item.get("ItemCode")); } 
+2
source

Source: https://habr.com/ru/post/911391/


All Articles