简体   繁体   English

如何从视图骨干传递回模型属性

[英]How to pass back model attribute from a view Backbone

I have a controller: 我有一个控制器:

            var layout = new LayoutView();
            App.holder1.show(layout);
            var my_view = new myView({id: options})
            layout.holder1.show();

            console.log(my_view.model.get('name')) <---- I want this

I want to get my_view.model.get('name') however, the issue is I get undefined . 我想get my_view.model.get('name')但是问题是我undefined I have console.log the model and it is populated ok, however I think it's because it's not fully loaded yet when I try the get. 我有console.log模型,它已填充好,但是我认为这是因为当我尝试获取时它尚未完全加载。

This is my current thisView: 这是我当前的thisView:

  var thisView = Backbone.Marionette.ItemView.extend({
            initialize: function (options) {
                this.model.fetch();
            },
            model: new myModel(),
            template: testExampleTemplate,
        });
        return thisView;

You'll have the object populated only after the success callback function: 仅在成功回调函数之后,才会填充对象:

initialize: function (options) {
  this.model.fetch({
    success: function(model){
      console.log(model.get('name'));
    };
  });
}

Listen for an event. 听一个事件。 "change" or "reset" will work. “更改”或“重置”将起作用。

viewInstance.model.on("change", function(){
    viewInstance.model.get("nameOfAttribute");
    // do something
});

http://backbonejs.org/#Events-catalog http://backbonejs.org/#Events-catalog

There's a few ways to approach this. 有几种方法可以解决此问题。 First, you could listen for a change event from the model in the view, and do whatever it is you need when the change event fires. 首先,您可以从视图中的模型中监听更改事件,并在更改事件触发时执行所需的任何操作。 If you need to do something no matter what, you have a couple of options: you could write an implementation for you model's parse method that fires an event your view listens for and does something in response, or you can do something in the success callback for the fetch method itself (passed as an option to fetch). 如果您无论如何都需要做一些事情,则有两种选择:您可以为模型的parse方法编写一个实现,该实现会触发视图监听的事件并做出响应以响应事件,或者您可以在成功回调中做一些事情用于获取方法本身(作为获取的选项传递)。 I can provide an example if I understand better which approach makes sense for your situation. 如果可以更好地理解哪种方法适合您的情况,我可以举个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Backbone:将模型属性传递给视图返回undefined - Backbone: Pass Model Attribute to a View returns undefined 要求。 如何将模型传递到视图(主干) - require. how to pass a model to a view (backbone) 从另一个视图调用视图并将其传递给Backbone模型的最佳方法 - Best way to call view from another view and pass it a model in Backbone 如何显示骨干模型集合中的视图 - how to display view from model collection in backbone 如何基于骨干模型的响应创建/更新骨干视图? - How to create / update a Backbone View based on a response from a Backbone Model? Backbone.js-将变量从路由传递到视图/集合/模型 - Backbone.js - Pass variable from route to view/collection/model 如何将模型从视图传递到控制器,将项目添加到列表,传递回视图 - How to pass Model from View to Controller, add items to list, pass back to view 如何将模型(数据)从一个视图传递到Backbone中的另一个视图并进行编辑/删除? - How to pass a model(data) from one view to another in Backbone and edit/delete it? 如何从另一个模型访问一个模型属性以传入 Rails 5 中的 javascript 视图部分? - How to access one model attribute from another model to pass in javascript view part in Rails 5? Backbone:如何从模型集合中保存模型的属性更改? - Backbone: How to save an attribute change of a model right from its collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM