自动生成ID与Ember&火力地堡 [英] Automatically generating IDs with Ember & Firebase
问题描述
我正在为模型生成ID。 Ember文档说Ember自动为其模型生成ID。
I'm trying to generate ID's for a model. Ember documentation says that Ember automatically generates IDs for its models.
但是,当我将数据传递给Firebase并尝试显示时,ID返回未定义。我的代码有问题吗?
But when I pass data to Firebase and try to display it, the IDs return undefined. Is there something wrong with my code?
<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);
},
}
});
推荐答案
当您调用 store.createRecord()
您有一个选项可以自动生成 id
(请参阅这里)最终,该责任被委派为一个适配器
。如果您的适配器具有 generateIdForRecord()
方法 - 将用于创建一个ID。所以,例如, FixtureAdapter
实现这个方法如下(参见:
When you call this.store.createRecord()
you have an "option" to have an id
autogenerated (see here) Ultimately though, that responsibility is delegated to an adapter
. If your adapter has generateIdForRecord()
method - this will be used to create an id. So, for example, FixtureAdapter
implements this method as follows (see here):
generateIdForRecord: function(store) {
return "fixture-" + counter++;
}
如果您使用的是 FirebaseAdapter
(这一个)它似乎不会生成一个 id
,所以如果你真的希望这个功能在客户端上发生,你需要自己来扩展它。但是,如果你这样做,你需要真正问自己。 ;)默认的 RestAdapter
不会为其模型生成ids。
If you are using the FirebaseAdapter
(this one) it doesn't look like that's generating an id
, so you would need to extend it and do it yourself if you really want this functionality to happen on the client. But you need to really ask yourself if you do. ;) The default RestAdapter
doesn't generate ids for its models either.
这篇关于自动生成ID与Ember&火力地堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!