1

我有一个 Ember.js (1.0.0) 应用程序,我正在尝试为其实现内置Ember.Select视图。

这部分应用程序显示了三个任务列表:inProgresscompletedunassigned。用户可以过滤其相应项目显示的任务。这就是Ember.Select视图的来源。但是,当我加载路线时,Ember 对我咆哮我给它的值的类型:

Assertion failed: The value that #each loops over must be an Array. You passed projects.all

Uncaught TypeError: Object projects.all has no method 'addArrayObserver'

Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.

我已经为此苦苦挣扎了好几个小时,尝试了下面代码的不同排列 - 我知道我一定遗漏了一些明显的东西,因为让这样一个简单的组件正常工作并不是那么困难。希望你们能指出我正确的方向。

这是我的路线:

Bee.TasksIndexRoute = Bee.Auth.Route.extend
    setupController: (ctrl) ->
        # get tasks
        Bee.Auth.send
            url: Bee.endpoint "/tasks"
        .done (tasks) -> 
            ctrl.set "tasks.all", tasks
        # get projects
        Bee.Auth.send
            url: Bee.endpoint "/projects"
        .done (projects) -> 
            ctrl.set "projects.owned", projects.owned
            ctrl.set "projects.participating", projects.participating
            ctrl.set "projects.all", projects.owned.concat projects.participating

这是我的控制器:

Bee.TasksIndexController = Ember.ObjectController.extend
    project: null
    content:
        tasks: 
            all: []
            inProgress: []
            completed: []
            unassgined: []
    projects: 
        all: []
        owned: []
        participating: []
    visible: (->
        ctrl = @
        # filter tasks here            
    ).property "project"

这是我的模板:

<script type="text/x-handlebars" id="tasks/index">
    <div class="center-pane">
        <div class="top_options">
            <div class="project_filter">
                <strong>Viewing: </strong>
                {{view Ember.Select
                   content=projects.all
                   optionValuePath='content._id'
                   optionLabelPath='content.title'
                   value=project
                   prompt='All Tasks'
                }}
            </div>
            <strong class="gold-gradient option_button">
                {{#link-to 'tasks.create' classNames='new_task'}}Create Task{{/link-to}}
            </strong>
        </div>
        <div class="col3">
            <div class="col-header in-progress light-gradient">
                <h3>In Progress</h3>
            </div>
            <div id="tasks_active_list">
                {{#if visible.inProgress.length}}
                    <ul>{{#each visible.inProgress}}{{view Bee.TaskListView}}{{/each}}</ul>
                {{else}}
                    <p class="no_projects">None</p>
                {{/if}}
            </div>
        </div>
        <div class="col3">
            <div class="col-header completed light-gradient">
                <h3>Completed</h3>
            </div>
            <div id="tasks_closed_list">
                {{#if visible.completed.length}}
                    <ul>{{#each visible.completed}}{{view Bee.TaskListView}}{{/each}}</ul>
                {{else}}
                    <p class="no_projects">None</p>
                {{/if}}
            </div>
        </div>
        <div class="col3">
            <div class="col-header unassigned light-gradient">
                <h3>Unassigned</h3>
            </div>
            <div id="tasks_unassigned_list">
                {{#if visible.unassigned.length}}
                    <ul>{{#each visible.unassigned}}{{view Bee.TaskListView}}{{/each}}</ul>
                {{else}}
                    <p class="no_projects">None</p>
                {{/if}}
            </div>
        </div>
    </div>
</script>

任何见解将不胜感激。我确实知道这Ember.Select是罪魁祸首,因为当我用简单的替换它时:

<select>
    {{#each projects.all}}
        <option value="{{_id}}">{{title}}</option>
    {{/each}}
</select>

...它呈现得很好-但是我需要使用,Ember.Select所以我可以将值绑定到project属性上TasksIndexController-因为我将使用它作为触发visible函数的可观察对象。

4

1 回答 1

1

尝试将 projects.all 预先设置为空。也许 ember select 与类上的 pojo 默认数组有问题。

Bee.TasksIndexController = Ember.ObjectController.extend
  project: null
    content:
      tasks: 
        all: []
        inProgress: []
        completed: []
        unassgined: []
  projects: 
    all: null
    owned: []
    participating: []
  visible: (->
    ctrl = @
    # filter tasks here            
  ).property "project"



setupController: (ctrl) ->
    # get tasks
    Bee.Auth.send
        url: Bee.endpoint "/tasks"
    .done (tasks) -> 
        ctrl.set "tasks.all", tasks
    # get projects
    Bee.Auth.send
        url: Bee.endpoint "/projects"
    .done (projects) -> 
        ctrl.set "projects.owned", projects.owned
        ctrl.set "projects.participating", projects.participating
        ctrl.set "projects.all", projects.owned.concat projects.participating

这是一个简化的示例:http ://emberjs.jsbin.com/aletIyU/3/edit

于 2013-10-27T22:51:24.690 回答