My model has one field that contains collection of items. I want to access that collection from a jquery from the view that is bound with this particular model.
how can I achieve this?
My model has one field that contains collection of items. I want to access that collection from a jquery from the view that is bound with this particular model.
how can I achieve this?
Here is a slick way to do it. Build up a JSON data object via a foreach loop. Loading you data via an ajax call would be better, but that is a diferent solution for a different day.
Here I assume you have a collection of items that at least has an Id, and Name property. You will want to make your @foreach loop a little better than mine because JSON does not really allow the extra ',' at the end of the last item.
var data = [ @foreach (var item in Model.MyCollection) { <text>{"key":"@item.Id", "value":"@item.Name"},</text> } ]; $.each(data, function(i) { alert(data[i].key + ": " + data[i].value); }); I do something like this, not sure how 'correct' it is, but it works and makes sense to me.
var view = Backbone.View.extend({ el: $('window'), render: function() { var that = this; var model = new myModel(); model.fetch({ success: function({ that.dataModel = model; }); }); } }); And then anywhere you can do this.dataModel and it should be your model