我正在尝试为模型生成ID。 Ember文档说Ember会自动为其模型生成ID。
但是当我将数据传递给Firebase并尝试显示它时,ID返回undefined。我的代码有问题吗?
<script type="text/x-handlebars" id="stack">
<div class='stack'>
<div class="container">
<div class="row">
<div class="span12" style="text-align:center; margin: 0 auto;">
{{#if isEditing}}
{{partial 'stack/edit'}}
<button {{action 'doneEditing'}}>Done</button>
{{else}}
<button {{action 'edit'}}>Edit</button>
{{/if}}
<h1>{{stack.title}}</h1>
<h2>at {{stack.date}} <small class='muted'>({{format-date date}})</small></h2>
<hr>
<div class='intro'>
{{stack.location}}
</div>
<div class='below-the-fold'>
{{stack.details}}
</div>
...
App.Router.map(function() {
// put your routes here
this.resource('stacks');
this.resource('stack', {path: 'stacks/:stack_id'}, function() {
this.route('edit');
this.resource('comments', function() {
this.route('new');
})
});
this.route('create');
this.resource('groups');
});
App.StackRoute = Ember.Route.extend({
model: function(params) {
return stacks.findBy('id', params.stack_id);
}
});
App.CreateController = Ember.ObjectController.extend({
init: function() {
this.set('stack', Ember.Object.create());
},
actions: {
createStack: function() {
var newStack = this.store.createRecord('stack', {
title: this.get('stack.title'),
location: this.get('stack.location'),
date: new Date().getTime(),
details: this.get('stack.details'),
});
newStack.save();
alert('newStack.title' + " is ready!");
},
cancelStackCreation: function() {
this.sendAction('cancel');
alert("Canceled stack creation.");
},
}
});
App.StackController = Ember.ArrayController.extend({
isEditing: false,
actions: {
edit: function() {
this.set('isEditing', true);
},
doneEditing: function() {
this.set('isEditing', false);
},
}
});
答案 0 :(得分:1)
当您拨打this.store.createRecord()
时,您有一个&#34;选项&#34;自动生成id
(请参阅here)最终,该职责委托给adapter
。如果您的适配器具有generateIdForRecord()
方法 - 这将用于创建ID。因此,例如,FixtureAdapter
实现此方法如下(请参阅here):
generateIdForRecord: function(store) {
return "fixture-" + counter++;
}
如果您使用的是FirebaseAdapter
(this one),那么它似乎不会生成id
,因此您需要对其进行扩展并执行此操作如果您真的希望在客户端上发生此功能,请自行处理。但你需要真的问自己是否这样做。 ;)默认RestAdapter
也不会为其模型生成ID。
答案 1 :(得分:0)
我的问题通过使用
解决了{{#link-to 'stack.index' this.stack.id}}
我能够根据ID链接到特定的网址。