Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干模型:如何添加自定义属性,该属性组合来自其他属性的数据并保持同步_Backbone.js - Fatal编程技术网

Backbone.js 主干模型:如何添加自定义属性,该属性组合来自其他属性的数据并保持同步

Backbone.js 主干模型:如何添加自定义属性,该属性组合来自其他属性的数据并保持同步,backbone.js,Backbone.js,取回我的收藏时,服务器会向我发送姓、名和配偶名(以及对问题不重要的其他属性) 我希望能够添加一个名为mailing\u name的属性,它本质上是: var setNameOnFormAttribute = this.get("last_name") + ", " + this.get("last_name"); if (!_.isNull(this.get("spouse_first_name")) || !_.

取回我的收藏时,服务器会向我发送配偶名(以及对问题不重要的其他属性)

我希望能够添加一个名为mailing\u name的属性,它本质上是:

var setNameOnFormAttribute = this.get("last_name") + ", " + this.get("last_name");

if (!_.isNull(this.get("spouse_first_name")) || !_.isEmpty(this.get("spouse_first_name"))) {
setNameOnFormAttribute += " & " + this.get("spouse_first_name");
}
我想确保如果任何基本属性(姓氏配偶名)因任何原因发生更改,则会更新自定义的邮件名属性,并更新使用邮件名属性的任何视图

我用的是木偶,如果有区别的话

编辑1 我的第一次尝试:

define([
    'backbone'
],
function( Backbone ) {
    'use strict';

    /* Return a model class definition */
    return Backbone.Model.extend({
        initialize: function (){
            console.log(this.attributes);
            
            this.bind("change:last_name", this.setNameOnFormAttribute);
            this.bind("change:first_name", this.setNameOnFormAttribute);
            this.bind("change:spouse_first_name", this.setNameOnFormAttribute);
        },

        setNameOnFormAttribute: function(){
            var name_on_form = this.get("last_name") + ", " + this.get("first_name");

            if (!_.isNull(this.get("spouse_first_name")) || !_.isEmpty(this.get("spouse_first_name"))) {
                name_on_form += " & " + this.get("spouse_first_name");
            }

            this.set("name_on_form", name_on_form);
        }
    });
});
如果ind-odd的话,那么initialize函数开头的console.log会显示所有属性都已设置。因此属性上的更改事件不会触发,因此我无法将新属性注入

我可能采取了错误的方法

编辑2以回答Bernardo的问题 它与提线木偶的观点联系在一起。集合是从复合视图中获取的,集合只定义了适当的模型

define([
    'backbone',
    'collections/tasks',
    'views/item/task',
    'views/item/tasklist_empty',
    'hbs!tmpl/layout/tasklist_tmpl'
],
function( Backbone, TasksCollection, TaskView, tasklistEmptyView, TasklistTmpl  ) {
    'use strict';

    return Backbone.Marionette.CompositeView.extend({

        initialize: function() {
            this.collection = new TasksCollection();
            this.collection.bind("reset", _.bind(this.render, this));
            this.collection.fetch({ reset: true });
        },

        itemView: TaskView,
        emptyView: tasklistEmptyView,

        template: TasklistTmpl,

        itemViewContainer: ".tasks"

    });

});


define([
    'backbone',
    'moment',
    'application',
    'models/task'
],
function( Backbone, moment, App, Task ) {
    'use strict';

    /* Return a collection class definition */
    return Backbone.Collection.extend({
        initialize: function() {
        },

        comparator: function(model) {
            return moment(model.get('received_date')).unix();
        },

        model: Task,

        url: App.apiUrl + '/tasks'

    });
});

如果您在这里查看主干源:

您可以看到模型属性是在主干中设置的。调用初始化前的模型构造函数

但是,这与您的案例中不触发的更改事件无关:您的更改事件应该在从服务器获取模型数据后触发,并且您的绑定应该触发要设置的复合属性


到目前为止,您的代码看起来还不错-您如何初始化模型、视图和获取数据?

已编辑

我看到您确实在使用
获取
,但正在使用
重置
选项。如果查看所调用的
add
函数的主干带注释代码,它基本上会将
silent:true
添加到下游发送的选项中。然后,这将被发送到
\u preparemodel
函数,并导致集合中的所有子模型都使用
静默:true
创建,因此您看到的是属性集,而从未看到更改事件。在使用重置选项执行
fetch
之后,您需要在模型上手动引发该事件

目前,我认为您只需要手动触发该事件,并可能在
setmailtoname
函数中修复您的逻辑,或者不修复该逻辑,以便只使用
_isEmpty
或将短路或更改为and。我不会在初始化中添加将mail\u设置为\u name的调用,因为如果将其更改为fetch without set等,它将触发两次

这里是一个新的与变化和他们的演示。还有一个用于测试的同步重定向

我基本上是把这个添加到你的收藏模型中。也许有更好的方法,但我只研究了几天主干

initialize: function () {
    this.bind("reset", function () {
        _.each(this.models, function (model) {
            model.setNameOnFormAttribute.apply(model, arguments);
        });
    });
编辑结束:剩下的只是信息。

我假设这些数据不是来自
获取
或其他什么,因为您正在检查
初始化
函数中的属性?这不会在
获取上执行,对吗

假设不是:

在映射属性后进行更改之前,这些事件不会触发,因此我在
initialize
函数中调用了
setNameOnForm
。在提供的代码中,我将
选项设置为
{silent:true}
,以便不发送“change”触发器,但您可能希望根据使用情况删除该触发器

假设它已被提取

要设置的功能可能有误吗?你能登录那里看看它是否被调用,它是否返回正确的值吗?我认为函数应该是下面的函数。您可能对
|
而不是
&
有问题

jsfiddle

我还添加了处理
选项
参数的功能[直接调用或从调用传递到观察集[ex.
aModel.set(“last_name”);
],然后通过
Backbone.Model.trigger
函数将其作为第三个参数发送到方法


我贴了一个有点黑的日志。我不知道如何使用木偶,对不起。我没有更新它的功能更改。

一些链接。它与木偶相连。我有一个复合视图,在它的视图中获取集合。