Ember.js:数据感知组件和组件内通信

时间:2014-09-16 23:02:38

标签: ember.js ember-data

Ember      : 1.4.1+pre.af87bd20 
Ember Data : 1.0.0-beta.7.f87cba88 
Handlebars : 1.3.0 
jQuery     : 2.1.0 

我在模板上有两个组件,一个是搜索向导,允许您将搜索字词添加到表格中,然后使用搜索字词构建一个'查询'返回类型的对象(让他们调用它们)'小部件'。

第二个组件是X类对象的查看器,它根据某些特征对它们进行分组。

我的路线是传统的ArrayController路线:

MyApp.WidgetsRoute = Ember.Route.extend(Ember.SimpleAuth.AuthenticatedRouteMixin,
  model: ->
    @store.find 'widgets'

setupController: (controller, model) ->
  controller.set 'content', model
  ...
)

这两个组件在模板(widgets.hbs)上单独运行,我有:

<h3>Widgets</h3>
{{search-widgets-by-attribute}}
{{grouped-widgets model=content}}
{{#link-to 'widgets.new'}}New Widget{{/link-to}}
{{outlet}}

分组窗口小部件组件正确显示路径中检索到的所有窗口小部件。问题是如何清除和设置控制器的模型,以便搜索结果(由search-widgets-by-attribute组件检索)反映在分组窗口小部件组件中。

这是我尝试过的:

1)将控制器模型传递给search-widgets-by-attribute

{{search-widgets-by-attribute model=content}}

在组件中,doSearch操作尝试设置模型:

MyApp.SearchWidgetsByAttributeComponent = Ember.Component.extend(
  ...
  actions:
    doSearch: ->
        component = @
        store = MyApp.__container__.lookup('store:main')
        store.searchByTypeAndUrl("widget", "/widgets/search",
          search:
            attribute_names: @.get('searchAttributes')
        ).then (searchResults) ->
          component.set('model', searchResults)
)

searchByTypeAndUrl返回一个Widgets数组。设置上述模型会导致:

结果:

Uncaught TypeError: Cannot read property 'firstChild' of null
firstNodeFor 
prependFunc 
DOMManager.prepend 
insertViewCollection
Ember.merge.ensureChildrenAreInDOM 
Ember.ContainerView.Ember.View.extend._ensureChildrenAreInDOM 
DeferredActionQueues.flush 
Backburner.end 
(anonymous function)

我还尝试使用Em.Array和Em.ArrayProxy以及Em.MutableArray来包装Widgets的普通JS数组,但无济于事。

2)将控制器传递给search-widgets-by-attribute

{{search-widgets-by-attribute widgetsController=controller}}

在组件中,doSearch操作尝试设置模型:

MyApp.SearchWidgetsByAttributeComponent = Ember.Component.extend(
  ...
  actions:
    doSearch: ->
        widgetsController = @.get('widgetsController')
        store = MyApp.__container__.lookup('store:main')
        store.searchByTypeAndUrl("widget", "/widgets/search",
          search:
            attribute_names: @.get('searchAttributes')
        ).then (searchResults) ->
          widgetsController.set('model', searchResults)
)

结果:

Uncaught TypeError: Cannot read property 'firstChild' of null
firstNodeFor 
prependFunc 
DOMManager.prepend 
insertViewCollection
Ember.merge.ensureChildrenAreInDOM 
Ember.ContainerView.Ember.View.extend._ensureChildrenAreInDOM 
DeferredActionQueues.flush 
Backburner.end 
(anonymous function)

3)使用find方法

设置模型

这才有效!但这不是我想要的:-(我从中得到的暗示是我似乎需要一个DS.RecordArray!

 MyApp.SearchWidgetsByAttributeComponent = Ember.Component.extend(
  ...
  actions:
    doSearch: ->
        store = MyApp.__container__.lookup('store:main')
        component = @
        store.find('specification').then (specifications) ->
          component.set('model', specifications)

后续步骤......

searchByTypeAndUrl的代码如下:

MyApp.Store = DS.Store.extend(
  revision: 11
  searchByTypeAndUrl: (type, url, params) ->
    self = this
    promise = Ember.Deferred.create()

    $.getJSON url, params, (payload) ->
      serializer = self.serializerFor(type)
      payload = serializer.extractArray(self, self.modelFor(type), payload)
      objects = self.pushMany(type, payload)
      promise.resolve objects

    promise
)

我已尝试将模型更改为路线中模型挂钩中的空数组,但即使在上面执行#3时我也会得到first child of null

我的最后一次尝试是让searchByTypeAndUrl返回RecordArray。任何建议都会受到欢迎。

0 个答案:

没有答案