【问题标题】:How does the view in backbone marionette know which model is associated with it?骨干木偶中的视图如何知道与哪个模型相关联?
【发布时间】:2013-01-03 06:27:27
【问题描述】:

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.view.md

在这个例子中:

<script id="my-template" type="text/html">
  I think that <%= showMessage() %>
</script> 

MyView = Backbone.Marionette.ItemView.extend({
  template: "#my-template",

  templateHelpers: {
    showMessage: function(){
      return this.name + " is the coolest!"
    }
  }

});

model = new Backbone.Model({name: "Backbone.Marionette"});
view = new MyView();
view.render(); //=> "I think that Backbone.Marionette is the coolest!";

我已尝试分析此代码,根据我对 Backbone 的理解,您必须指定视图与哪个模型相关联。我尝试理解 Marionette 视图,但我不知道文档的哪一部分或在此示例中显示视图如何知道 this 指的是新创建的模型。或者这只是一个错字?

【问题讨论】:

    标签: marionette


    【解决方案1】:

    该示例中有一个错误。它应该显示:

    
    model = new Backbone.Model({name: "Backbone.Marionette"});
    
    view = new MyView({
      model: model
    });
    
    view.render(); //=> "I think that Backbone.Marionette is the coolest!";
    

    我会更新文档来解决这个问题

    【讨论】:

    • 啊,我希望是这样。如果不是我会很困惑。感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多