简体   繁体   English

Ember.js:从ArrayController中删除对象

[英]Ember.js: Removing an object from an ArrayController

I have an ArrayController that uses an itemController to manage a set of components. 我有一个使用itemController管理一组组件的ArrayController。 The ArrayController in this example represents a book, and the item controllers represent the pages of the book. 在此示例中,ArrayController代表一本书,项目控制器代表该书的页面。 Each page has a unique ID that is generated by a App.generateId function. 每个页面都有一个由App.generateId函数生成的唯一ID。 The model for a page is App.Page . 页面的模型为App.Page It does not use Ember-Data. 它不使用Ember-Data。

// ArrayController
App.BookController = Ember.ArrayController.extend({
  itemController: 'page',

  addPage: function (options) {
    return this.pushObject(
      App.Page.create({
        uid: App.generateId('page')
      })
    );
  },

  removePage: function (page) {
    console.log('Content array: ' + this.get('content'));
    console.log('Content array length: ' + this.get('content').length);
    console.log('Page UID: ' + page.get('uid'));
    console.log('Page at content[0]: ' + this.objectAt(0));
    console.log(this.findProperty('uid', page.get('uid')));
  }
});


// ItemController
App.PageController = Ember.Controller.extend({});

// Page Model
App.Page = Ember.Object.extend({
  uid: ''
});

The addPage method woks fine. addPage方法可以正常工作。 It gets called and successfully creates+adds a page to the book and wraps it in the item controller. 它被调用并成功创建并添加页面到书中并将其包装在项目控制器中。

I am having trouble with the removePage function. 我在使用removePage函数时遇到麻烦。 Here is the output of the console logs that I have in that method (after adding 1 page, and trying to remove it): 这是我在该方法中拥有的控制台日志的输出(添加1页并尝试将其删除之后):

console.log('Content array: ' + this.get('content'));
//--> Content array: <(subclass of App.Page):ember667>

console.log('Content array length: ' + this.get('content').length);
//--> Content array length: 1

console.log('Page UID: ' + page.get('uid'));
//--> Page UID: page-cm6p6pmmt5aq50i9ej7ona1dsand3mf

console.log('Page at index 0: ' + this.objectAt(0));
//--> Page at index 0: <App.PageController:ember668>

console.log(this.findProperty('uid', page.get('uid')));
//--> undefined

I am confused on two points: 我对两点感到困惑:

  1. When I output the contents of content on the ArrayController, it shows the single page as a subclass of the model (App.Page) with Ember ID 667. When I look at the object at index 0 of the content array with this.objectAt(0) , it shows me not the model, but the item controller for the page, with Ember ID 668. Shouldn't these two outputs be the same? 当我在ArrayController上输出内容的content时,它会将单个页面显示为Ember ID为667的模型的子类(App.Page)。当我使用this.objectAt(0)内容数组索引0处的对象时this.objectAt(0) ,它不是显示模型,而是显示页面的项目控制器 ,Ember ID为668。这两个输出不应该相同吗?

  2. Why does the findProperty method return undefined ? 为什么findProperty方法返回undefined The same happens for other methods as well, such as findBy . 其他方法也是如此,例如findBy I see that Ember.ArrayController extends Ember.ArrayProxy, which then in turn uses Ember.MutableArray. 我看到Ember.ArrayController扩展了Ember.ArrayProxy,然后又使用Ember.MutableArray。 I've read that you can't do this sort of stuff to immutable arrays out of Ember-Data. 我读到您无法对Ember-Data中的不可变数组执行此类操作。

Any help is extremely appreciated. 任何帮助都非常感谢。

When you iterate your ArrayController, or try and access an item from the collection via the ArrayController, it returns the model wrapped with the item controller. 当您迭代ArrayController或尝试通过ArrayController访问集合中的项目时,它将返回包装有项目控制器的模型。 If you iterate or try fetching from the model/content on the controller it just returns the model. 如果您迭代或尝试从控制器上的模型/内容中获取数据,它将仅返回模型。

Generally when using the ArrayController and ObjectController you don't need to use content or model . 通常,在使用ArrayControllerObjectController时,不需要使用contentmodel This is due to both controllers being proxy controllers which proxy requests down to the model/content it's decorating. 这是由于两个控制器都是代理控制器,它们代理请求到其装饰的模型/内容。

console.log('Content array length: ' + this.get('length'));
console.log('Page UID: ' + page.get('uid'));
console.log('Page at content[0]: ' + this.objectAt(0));
console.log(this.findBy('uid', page.get('uid')));

findProperty is deprecated, and you should be using findBy (despite the undefined issue, I'll try and replicate real quick). findProperty已被弃用,你应该使用findBy (尽管不确定的问题,我会尝试和复制真正的快)。

Additionally your actions addPage and removePage should be in an actions hash. 另外,您的动作addPageremovePage应该在动作哈希中。

actions: {
  addPage: function(){...},
  removePage: function(){...},

}

The Problem 问题

The real problem is your itemController is extending Ember.Controller . 真正的问题是您的itemController扩展了Ember.Controller Ember.Controller is not a proxy controller, it is a controller that is assumed to have no model backing it, so it won't proxy requests down to it. Ember.Controller不是代理控制器,它是假定没有模型支持的控制器,因此它不会代理请求。 You'll just need to change this to Ember.ObjectController and you'll be good. 您只需要将其更改为Ember.ObjectController可以了。

Example: 例:

http://emberjs.jsbin.com/wulosufu/2/edit http://emberjs.jsbin.com/wulosufu/2/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM