Marionette.CompositeView和Marionette.ItemView之间的事件 [英] Events between Marionette.CompositeView and Marionette.ItemView

查看:83
本文介绍了Marionette.CompositeView和Marionette.ItemView之间的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模块itemView.jsListView.js.
当我获取数据时,一切正常.

I have two modules itemView.js and ListView.js.
Everything works when I fetch the data.

当我更改相应的模型时,问题出在item view(1)上.

The problem is about the item view (1) when I change the corresponding model.

a)ListView.js(2)显示模型具有closed值等于false(3)的所有对象

a) the ListView.js (2) displays all the object which models have closed value equal to false (3)

b)(1)中的动作closeTask更改模型的值
closed: falseclosed: true

b) the action closeTask in (1) changes the value of the model
from closed: false to closed: true

c)当b)发生时,什么都没有改变,
但是,如果我重新加载页面,则会得到正确的结果(不会显示closed值等于true的模型).

c) when b) occurs nothing change,
but if I reload the page I get the right results (the model having the closed value equal to true is not displayed).

我应该如何解决此问题?

How should I fix this issue?

(1)

// itemView.js
var itemView = Marionette.ItemView.extend({

    initialize: function () {
        this.model.on('change', this.render, this);
    },

    events: {
        'click #close': 'closeTask'
    },

    template: itemTemplate,

    tagName: 'li',

    closeTask: function () {
        if (!this.model.get('closed')) {
            this.model.save({
                closed: true
            });
        }
    }

});


(2)


(2)

// ListView.js
var ListView = Marionette.CompositeView.extend({

    template: listTemplate,

    itemView: itemView

});


(3)


(3)

// Collection
myCollection.attributes = [
    {
        id: 1,
        name: 'bar'
        closed: false
    },
    {
        id: 2,
        name: 'bar2'
        closed: false
    },
    ….
];


P.S .:


P.S.:

当我获取集合时,服务器仅向我提供其close属性等于false的模型.

When I fetch the collection, the server give me just the models which have the closed attribute equal to false.

app.addInitializer(function () {
    myCollection = new MyCollection();
    myCollection.fetch();
});

推荐答案

我没有使用Marionette,但我一直在使用Backbone,我的想法是Marionette不会刷新模板或类似的东西.

I did not work with Marionette but I have been working with Backbone, my thought is that Marionette is not refreshing the template or something like it.

如果您尝试这样做会怎样?

What happens if you try this?

// itemView.js
var itemView = Marionette.ItemView.extend({

    initialize: function () {
        this.model.on('change', this.render, this);
    },

    onRender : function(){
       //verify your model:
       console.log( this.model.toJSON() );
       if (this.model.get('closed')) {
          this.$el.fadeOut();//bye bye item
       }
    },

    events: {
        'click #close': 'closeTask'
    },

    template: itemTemplate,

    tagName: 'li',

    closeTask: function () {
        if (!this.model.get('closed')) {
            this.model.save({
                closed: true
            });
        }
    }

});

这篇关于Marionette.CompositeView和Marionette.ItemView之间的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆