我们有一个示例ember app我们CRUD配方。对于我们创建新配方的路径,如果我们将属性传递给createRecord,如下所示:
Cookbook.RecipesNewRoute = Ember.Route.extend
model: ->
Cookbook.Recipe.createRecord(title: "blank")
配方会立即显示在屏幕左侧的列表中。这是显示我的意思的jsbin
但是,如果我创建没有args a la
的食谱Cookbook.Recipe.createRecord()
在编辑其中一个属性之前,我没有看到配方出现在列表中。即使我已经指定了默认值,也会发生这种情况,正如jsbin所证明的那样。
我的问题是:为什么会发生这种情况,如何创建一个没有指定参数并且仍然立即显示的记录?
答案 0 :(得分:0)
在创建空记录后,查看你的jsbin是你唯一遗漏的是@get('store').commit()
。
这是一个例子:
Cookbook.RecipesNewRoute = Ember.Route.extend
model: ->
Cookbook.Recipe.createRecord()
@get('store').commit()
在这里你的工作jsbin。我已将括号中的id添加到模板中,以便您可以看到记录是否已正确创建。
编辑在您发表评论后,我对 jsbin 进行了一些更改,基本上我改变的是:
Cookbook.RecipesNewRoute = Ember.Route.extend
setupController: (controller) ->
controller.startEditing()
Cookbook.RecipesNewController = Ember.ObjectController.extend
startEditing: ->
@controllerFor('recipes').pushObject(Cookbook.Recipe.createRecord())
为方便起见,我添加了方法startEditing
(您可以随意调用它)。
在startEditing
方法中,您可以看到我们获取recipesController
并向content
数组添加新记录,这会导致新配方即使未提交到商店也会显示。试一试。
希望有所帮助
答案 1 :(得分:0)
所以看起来很漂亮,但这就是我如何按照我想要的方式工作:
Cookbook.RecipesNewRoute = Ember.Route.extend
model: ->
recipe = Cookbook.Recipe.createRecord()
@controllerFor('recipes').get("content").addReference(recipe.get("_reference"))
recipe
我不知道这是否是犹太洁食,但似乎有效。欢迎提供反馈和改进。我不会将此标记为几天的答案,让其他人有机会想出更好的东西。
答案 2 :(得分:0)
查看HTML,您将看到已创建<li>
元素,但此模型的title
属性为空,因此新配方不会显示在列表中。
<li>
<a id="ember413" class="ember-view" href="#/recipes/ember369">
<script id="metamorph-12-start" type="text/x-placeholder"></script>
<script id="metamorph-12-end" type="text/x-placeholder"></script>
</a>
</li>