【问题标题】:Backbone call model function骨干呼叫模型功能
【发布时间】:2014-05-29 16:35:19
【问题描述】:

我有一个系列,我从这个系列中选择了一个模型。在这个模型中,我之前定义了一个函数。如何调用从集合中选择的模型的这个函数?

我在哪里调用模型函数(但未定义):

 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);// is well defined
        console.log("retweet");
        console.log(this.model.reTweet()); //here I try to call model function
      },




  });

这是模型:

      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;

                }
            );




 }



  });

这是调用先前视图的主视图。它迭代集合并将模型传递给视图。

 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;



    },

【问题讨论】:

  • 此视图接受来自另一个视图的模型,该模型迭代集合。如果我的 console.log(this.model) 定义明确。如果我创建一个 var=new Tweet(); tweet.reTweet(),工作正常。但我不想创建另一个模型。@clintronx
  • console.log(this.model.reTweet) 从视图中打印出什么?
  • 你能添加视图的创建和你设置模型的位置吗?
  • 看起来您的 Collection 可能没有创建 Tweet 类型的模型,而只是将响应解析为基本的主干模型。请也提供您的收藏。
  • 我添加了集合迭代并传递给视图。 @clintronx

标签: javascript backbone.js


【解决方案1】:

您的收藏似乎应该指定模型类型。 See docs for more info

更新:

From annotated source

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);
};

要将您的模型与集合相关联 - 不是由集合通过 fetch 创建的 - 只需在创建集合时将集合传递给模型(假设 this.collection 是您的实际集合实例):

new Model({collection: this.collection})

【讨论】:

  • 只保留一个奇怪的东西:this.model.collection 有时是定义的,有时不是@clintronx
  • 我需要看看您的收藏是如何填充的。似乎您可能正在将模型添加到集合中,而这些模型不是在获取期间“由”集合创建的。我相信这将导致无法使用collection 属性集创建模型。尝试通过 new Model() 上的选项块手动设置它。模型的注释来源:f (options.collection) this.collection = options.collection; 集合选项除外
  • 是的,你是对的......我不明白在哪里插入'f(options.collection)this.collection = options.collection;' @clintronx
猜你喜欢
  • 1970-01-01
  • 2013-10-14
  • 2020-04-18
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
相关资源
最近更新 更多