繁体   English   中英

获取更改的“ this.model”属性值

[英]Get the changed “this.model” attribute value

一个Backbone.js noob问题。 目前正在使用的代码如下所示

我的数据模型模型:

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
    var DataModel = Backbone.Model.extend({
        // Defaults attribute values
        defaults : {
            completed : false,
        },
        url : 'path/to/my/data.json',
    });
    return DataModel;
});

我还有一个名为ServiceModel模型,它是DataModel的实例,看起来像这样

define(['jquery', 'underscore', 'backbone', '../models/DataModel'], function($, _, Backbone, DataModel) {

    // SupplierService as an Object Literal.
    SupplierService = {};

    // Instatiate the DataModel()
    var ServiceModel = new DataModel();
    var that = ServiceModel;

    ServiceModel.fetch().done(function() {
        data = ServiceModel.toJSON();

        SupplierService.figures.calculate(data)
    });

    SupplierService.figures = (function() {
        // I set completed to true here
        function constructor(d) {
        // some work done here
            that.set({
                completed : true,
            });
        }

        return {
            calculate : function(d) {
                constructor(d)
            },
        }
    })();

    return ServiceModel;
});

我的观点 :

define(['jquery', 'underscore', 'backbone', '../../models/ServiceModel'], function($, _, Backbone, ServiceModel) {
    var AppVIew = Backbone.View.extend({
        model : ServiceModel,
        initialize : function() {
            console.log(this.model.get('completed'));
        },
    });
    return AppVIew;
});



我的输出显示false而不是true 这是我打印this.model时的this.model

在此处输入图片说明

为什么当我this.model.get('completed')我得到false

我在做正确的事情? 任何帮助/建议将不胜感激,以走得更远。

提前致谢。

View:initialize您获得了默认模型值,然后在几秒钟内ServiceModel.fetch() 完成了请求并使用true更新了模型值

依我看来:
1从SupplierService移出DataModel
2以与DataModel相同的方式重写SupplierService
3传递SupplierService作为对AppVIew依赖
AppView:initialize 4 AppView:initialize放置

serviceModel = new ServiceModel();
supplierService = new SupplierService();

serviceModel.fetch().done(function() {
   var data = serviceModel.toJSON();

   supplierService.figures.calculate(data)
});

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM