Ember js:parentViewDidChange没有被触发

时间:2013-04-08 09:21:25

标签: view ember.js

如果有一个小游戏,必须在较大图像的右侧热点上拖动图像。

小图像在containerView内,热点也是ContainerViews。 当我将图像放在热点上时,我在我的拖放混合中使用以下代码来移动dom中的图像:

Player.Droppable = Ember.Mixin.create({
    drop: function(event) {
        //get the view that was dragged
        var viewId = event.originalEvent.dataTransfer.getData('Text');
        var view = Ember.View.views[viewId];

        //log the parent-view: App.AnswerListView
        console.log(view.get('parentView').constructor);

        //detach the view from the original containerView
        var parentView = view.get('parentView');
        parentView.removeObject(view);

        //attach it to the hot-spot-containerview
        this.addObject(view);

        //logging this gives a different result: App.HotspotView
        console.log(view.get('parentView').constructor);

        event.preventDefault();
        return false;
    }
});

我正在拖动的视图是App.AnswerView。我对文档的期望是,AnswerView上的函数parentViewDidChange被触发,但是没有发生:

App.AnswerView = Ember.View.extend(App.Draggable, {
    templateName: "answer",
    classNameBindings: [':answer', 'this.content.isSelected:selected'],
    click: function(evt){
        this.get('controller').send('answerClicked', this.content);
    },
    parentViewDidChange: function(){
        this.get('controller').send('answerMoved', this.content);
    },
});

文档说:在parentView属性发生更改时调用。在我的情况下,它被改变了。这是一个错误,还是我在这里遗漏了什么?

TIA

2 个答案:

答案 0 :(得分:1)

此问题是一个错误,并在1.0.0决赛中得到解决 https://github.com/emberjs/ember.js/issues/2423

答案 1 :(得分:0)

pushObject和removeObject是继承自Ember.ContainerView扩展的Ember.MutableArray Mixin的方法。如果你看看Ember的ContainerView(https://github.com/emberjs/ember.js/blob/v1.0.0-rc.2/packages/ember-views/lib/views/container_view.js#L15)的源代码,你会发现ContainerView没有覆盖那些方法,因此它们只操纵它的childViews数组,而不是视图的父视图。 你应该在这里找到操纵'_parentView'属性的方法:(https://github.com/emberjs/ember.js/blob/v1.0.0-rc.2/packages/ember-views/lib/views/view.js#L2018) - 在Ember.View的实现中。 简而言之,请使用:

  • removeChild而不是removeObject,从ContainerView中删除子视图
  • createChildView + pushObject而不是'addObject',如果要将新的子视图添加到ContainerView。

示例代码:

Player.Droppable = Ember.Mixin.create({
    drop: function(event) {
        //get the view that was dragged
        var viewId = event.originalEvent.dataTransfer.getData('Text');
        var view = Ember.View.views[viewId];

        //log the parent-view: App.AnswerListView
        console.log(view.get('parentView').constructor);

        //detach the view from the original containerView
        var parentView = view.get('parentView');
        parentView.removeChild(view);

        //attach it to the hot-spot-containerview
        this.createChildView(view);
        this.pushObject(view);

        //logging this gives a different result: App.HotspotView
        console.log(view.get('parentView').constructor);

        event.preventDefault();
        return false;
    }
});