Automatically generating IDs with Ember & Firebase

浪子不回头ぞ 提交于 2019-12-24 14:13:08

问题


I'm trying to generate ID's for a model. Ember documentation says that Ember automatically generates IDs for its models.

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);
        },
    } 
});

回答1:


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++;
}

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.




回答2:


My problem was solved by using

{{#link-to 'stack.index' this.stack.id}}

I was able to link to specific url based on the id.



来源:https://stackoverflow.com/questions/29017402/automatically-generating-ids-with-ember-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!