Get Object From View To Display In Text! Plugin Of Backbone Project
I am trying to display customer's sale history in a tab. When customer first click in to History tab, the data is displayed from Local Storage of the browser, other wise if the cu
Solution 1:
Recommend you to use Backbone.Model and Backbone.Collection?
Lets expect Model and Collection like this:
varprofileModel=Backbone.Model.extend({defaults: {
QuotationNumber:0,
InvoiceNumber:0,
QuotationDate:'00-00-00'
}
});varprofileCollection=Backbone.Collection.extend({model:profileModel});
Then lets update the views:
var profileItemView = Backbone.View.extend({
tag: 'tr',
template: _.template(CustomerProfile),
render: function(){
this.$el.html( this.template(this.model.toJSON()));
}
});
var profileCollectionView = Backbone.View.extend({
tagName: 'table',
render: function(){
this.collection.each(function(profile){
var itemView = newPersonView({ model: profile });
this.$el.append(itemView.render().el);
}, this);
}
});
Then lets update the template. Expect CustomerProfile as
<td><%= QuotationNumber %></td><td><%= InvoiceNumber %></td><td><%= QuotationDate %></td>
Highly recommend you to checkout this post its very useful in case of rendering collections in table view.
So to reach you goal you should listen the click on profileCollectionView and set data to collection from localStorage or from server json. You may need to override render() method in case of async json. You can use events 'fetch' and 'reset' to manage it:
var profileCollectionView = Backbone.View.extend({
tagName: 'table',
initialize: function(){
this.collection = newprofileCollection();
this.collection.on('reset', this.render, this);
this.collection.fetch({reset: true});
},
events : {
'click .btn' : 'resetCollection'
},
resetCollection : function(){
var saleHistoryObj = saleHistory.getSaleHistoryByID('877-04');
this.collection.reset(saleHistoryObj);
},
render: function(){
this.collection.each(function(profile){
var itemView = newPersonView({ model: profile });
this.$el.append(itemView.render().el);
}, this);
}
});
// lets create a view var view = newprofileCollectionView({ el : '.some_node' });
Post a Comment for "Get Object From View To Display In Text! Plugin Of Backbone Project"