0

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

小图像在 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

4

2 回答 2

1

This issue was a bug and got resolved in the 1.0.0 final https://github.com/emberjs/ember.js/issues/2423

于 2013-09-17T13:34:26.087 回答
0

pushObject and removeObject are methods, inherited from the Ember.MutableArray Mixin, that the Ember.ContainerView extends. If you look at Ember's source code for ContainerView (https://github.com/emberjs/ember.js/blob/v1.0.0-rc.2/packages/ember-views/lib/views/container_view.js#L15), you will see that ContainerView does not override those methods, thus they only manipulate its childViews array, not the view's parent view. You should find methods that manipulate the '_parentView' property here instead: (https://github.com/emberjs/ember.js/blob/v1.0.0-rc.2/packages/ember-views/lib/views/view.js#L2018) - in the Ember.View's implementation. So in short, use:

  • removeChild instead of removeObject, to delete a child view from your ContainerView
  • createChildView + pushObject instead of 'addObject', if you want to add a new child view to a ContainerView.

Example Code:

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;
    }
});
于 2013-04-08T10:49:10.103 回答