javascript - Backbone call model function -
i have collection, collection i've selected model. in model i've defined function. how can call function of model selected collection?
where call model function (but undefined):
var singlehomepostview = backbone.view.extend({ tagname: "li", template: handlebars.compile(template), events: { "click #retweet": "retweet", }, initialize: function () { // console.log(this.model); this.model.bind("change", this.render, this); this.model.bind("destroy", this.close, this); }, render: function (eventname) { var ad = this.model.tojson(); ad.cid = this.model.cid; $(this.el).html(this.template(ad)); return this; }, retweet: function () {//here---------- console.log(this.model);// defined console.log("retweet"); console.log(this.model.retweet()); //here try call model function }, });
and model:
var tweet = backbone.model.extend({ retweet: function(){ var params = { user_id: 11265832, //screen_name:"brad" page:1, count:2, }; cb.__call( "statuses_usertimeline", params, function (reply) { //gestire rate limit error 429 console.log(reply); // return reply; } ); } });
this main view call view. iterate on collection , pass model view.
loadresults: function (eventname) { //this.showspinner(); console.log("homepostview"+this.page); this.isloading === true; //console.log(this.integratedcollection); _.each(this.integratedcollection.last(this.integratedcollection.length).reverse(), function(model){ $(this.el).append(new singlehomepostview({ model: model }).render().el); },this); this.isloading === false;; return this; },
seems collection should specify model type. see docs more info
update:
var model = backbone.model = function(attributes, options) { var attrs = attributes || {}; options || (options = {}); this.cid = _.uniqueid('c'); this.attributes = {}; if (options.collection) this.collection = options.collection; if (options.parse) attrs = this.parse(attrs, options) || {}; attrs = _.defaults({}, attrs, _.result(this, 'defaults')); this.set(attrs, options); this.changed = {}; this.initialize.apply(this, arguments); };
to associate model collection - not created collection via fetch - pass collection model when create (assumes this.collection actual collection instance):
new model({collection: this.collection})
Comments
Post a Comment