我正在学习本教程,但教程是针对对象控制器的。在数组控制器中,如何正确传入文本字段的对象,以便触发该模型对象的更新?
现在我可以双击,然后键入一些值,如果我按Enter键,我会得到值加上未定义的方法集。
Uncaught TypeError: Object asdasdasdasdasd has no method 'set'
我猜它正在将原始值传递给控制器,然后尝试运行方法。如何让它通过实际模型?
查看:
<ul>
{{#each}}
<li {{bind-attr class="isEditing:editing"}} {{action "editWorkout" this on="doubleClick"}}>
{{#if isEditing}}
{{view Ember.TextField class='edit' action="updateWorkout"}}
{{else}}
{{#link-to 'workout' this}} {{title}} {{/link-to}}
{{/if}}
</li>
{{/each}}
<li>
{{newWorkoutName}}
</li>
</ul>
控制器:
EmberWorkouts.WorkoutsController = Ember.ArrayController.extend
actions:
editWorkout: (workout) ->
workout.set('isEditing', true)
createWorkout: ->
title = @get('newWorkoutName')
workout = @store.createRecord('workout', title: title)
@set('newWorkoutName', '')
workout.save()
updateWorkout: (workout) ->
workout.set('isEditing', false)
workout.save()
isEditing: false
如果你想调查,请点击这里:https://github.com/ecl1pse/ember-workouts/tree/master/app
答案 0 :(得分:1)
您可以在itemController
中指定each
,然后对列表中的每个项目使用ObjectController
。
{{#each itemController="workout"}}
<li {{action editWorkout on="doubleClick"}}>
<!-- Other stuff goes here -->
</li>
{{/each}}
EmberWorkouts.WorkoutsController = Ember.ObjectController.extend({
editWorkout : function(){
this.set('isEditing', true);
}
});
这是一个总体思路的JSBin:http://jsbin.com/ucanam/1038/edit