lundi 22 mai 2017

Add column to existing ember model

Wow this is hard to find.

I have an existing model in ember and I would like to add a new column. I have't been able to see how to generate this from the CLI, so have manually added it to my component.js and models/post.js. I've added the field to my form and the handlebar to my view. Checking Firebase I can confirm I'm not updating the field.

In Rails I would simply run rails generate migration add_snippet_to_posts snippet:string but doing this in Ember just creates a new model.

model/post.js

import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr('string'),
  author: DS.attr('string'),
  createdDate: DS.attr('date'),
  text: DS.attr('string'),
  snippet: DS.attr('string') #<=manually added this.
});

component.js

import Ember from 'ember';

export default Ember.Component.extend({
  actions: {
    createPost: function (model) {
      this.sendAction('createPost', model);

      // Clear each input field
      this.set('newPost.title', null);
      this.set('newPost.author', null);
      this.set('newPost.text', null);
      this.set('newPost.snippet', null); #<= manually added this
    }
  }
});

How do I do this?

Solved

Needed to update routes/index.js too:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function () {
    return this.store.findAll('post');
},
actions: {
  createPost: function (model) {
    let post = this.store.createRecord('post', {
      title: model.title,
      text: model.text,
      author: model.author,
      snippet: model.snippet, # <= add this too
      createdDate: new Date()
    });
    post.save();
  }
}

});




Aucun commentaire:

Enregistrer un commentaire