【问题标题】:Backbone listen to nested models/collections?骨干听嵌套模型/集合?
【发布时间】:2014-05-07 14:19:43
【问题描述】:

所以下面只是我在做的一个粗略的想法,集合是嵌套的,当this.collection.get('players') 发生变化时我需要调用getMax()

module.exports = LeadersCollectionView = Marionette.CompositeView.extend({
    className: 'live-stats',
    template: require('../../../templates/leaders/live-stats.hbs'),
    initialize: function() {
        this.listenTo(this.collection, 'change', this.render);
        //this.listenTo(this.collection.get('players'), 'change', this.render);
    },
    getMax : function(attribute) {
        console.log('getMax called')
        var home = this.collection.get('5368dcc1227a937829b2cb4a').players.models;
        var away = this.collection.get('5368dcd9227a937829b2cb4c').players.models;
        leaders = new PlayersCollection(home.concat(away))
        return leaders.max(function(leader) {
            return leader.get(attribute);
        });
    },
    templateHelpers:function(){
        return {
            maxPoints: this.getMax('points').get('player_name')
        }
    },
    itemView: leadersView,
    appendHtml: function(collectionView, itemView){
        collectionView.$('.live-data .created-teams').append(itemView.el);
    }
});

我使用的是骨干关系,然后决定放弃它,现在我想我可能再次需要它,但希望有更简单的方法。

因此,虽然可能有更好的方法来编写 getMax() 函数(显然静态团队目前已传入)如果我无法更新 getMax() 当点在球员模型。

所以我的问题是,我可以在没有骨干关系的情况下监听嵌套模型的变化吗?我该怎么做?

【问题讨论】:

    标签: javascript backbone.js marionette


    【解决方案1】:

    看看Backbone.DeepModel:你可以订阅 change:players.* 事件

    或坚持骨干关系。

    【讨论】:

    • 好吧,我绝对倾向于回到其中之一,无论我如何编写 getMax() 函数,我仍然需要以某种方式听取更改以更新该函数,所以是的,肯定需要那个。我认为使用 DeepModel 不合适,我一直在使用它,直到我没有找到理由,现在我有了,谢谢!
    • 我认为 deepmodel 的配置会更少,而且更轻量级我看一下,我正在努力保持最小化和干净。
    【解决方案2】:

    我只是想为有需要的人提供一个自制的解决方案。本质上,您只需在模型声明中创建一个新方法,该方法通常允许您在触发属性时更新属性,以便您可以利用 model.on("change:my.nested.attr");

    // Create your model; inside create a new method that will
    // set nested attributes (ie: setNestedAttr())
    var nestedBackboneAttributeSample = Backbone.Model.extend({
        defaults: {
            id: null,
            nestedProperties: {
                name: null,
                age: null,
                email: null
            }
        },
        /**
         * [setNestedAttr Set nested attribute and fire change event]
         * @param {[type]} nestedObj [object inside of this.attributes]
         * @param {[type]} property  [property inside of nestedObj]
         * @param {[type]} value     [value to replace nestedObj[property] with]
         */
        setNestedAttr: function(nestedObj, property, value) {
    
            // get current nested obj from this.attributes
            var ref = this.get(nestedObj)
                triggerString = "change:" + nestedObj + "." + property;
    
            console.log("Nested Object retrieved =>");
            console.log(ref);
    
            // if this.attributes.nestedObj has the property and
            // value is defined
            if (ref.hasOwnProperty(property) && value != undefined) {
    
                ref[property] = value;
                this.set({ nestedObj: ref });
                // manually trigger.. backbone doesn't monitor nested attrs
                this.trigger(triggerString);
    
            }
    
        }
    });
    
    // now, let's run some sampel code to give you 
    // an idea of how to use this.
    
    var x = new nestedBackboneAttributeSample();
    
    // setup event handler for on change w/specific nested attr
    x.on("change:nestedProperties.name", function() {
        console.log("this event was fired!");
    });
    
    x.setNestedAttr("nestedProperties", "name", "Github")
    // output => "this event was fired!"
    

    【讨论】:

    • 很好,希望我早点遇到这个问题,我用两个单独的集合构建了这个应用程序。我发现这样做确实更好。当您弄清楚如何通过 ID 关联模型时,它会变得更容易。
    【解决方案3】:

    我无法让 deepmodel 或 nestedmodels 工作,但这似乎是一个很棒的解决方案,它是如此简单,但我没想到会这样使用它。

    this.collection.each(function(team) {
        team.players.bind('change', this.render);
    }, this);
    

    【讨论】:

    • 我建议在集合更改的情况下监听添加、删除事件:team.players.bind('add', this.render); team.players.bind('remove', this.render);
    • 谢谢,我会这样做,但总会有一些警告,所以希望这能坚持下去,我正在对其进行测试,并且我确信在监听这些事件时会派上用场。
    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多