Create empty model from controller property


#1

Hi

I’m trying to go through the pluralsight tutorial about Ember.js fundamentals but using ember-cli (I uninstalled ember-data). My app is called GithubExplorer and I have one problem.

I have a simple model in app/models/issue.js

import Ember from 'ember';
export default Ember.Object.extend({
  title: "",
  body: ""
});

Then I have a controller in app/controllers/repository/newissue.js which tries to create an issue object, like this:

import Ember from 'ember';

export default Ember.Controller.extend({
  needs: ['repository'],
  repo: Ember.computed.alias('controllers.repository'),
  issue: function() {
    return GithubExplorer.Issue.create(); // This gives error
  }.property('repo.model'),
  actions: {
    submitIssue: function() {
      var issue = this.get('issue');
      // Do stuff...
    }
  }
});

When this is run GithubExplorer.Issue.create() I get the error Uncaught TypeError: Cannot read property 'create' of undefined. So, GithubExplorer.Issue is undefined. How do I access the “model” and create it from the issue function?


#2

Ah, Ok so I had to import it first in app/controllers/repository/newissue.js like this:

import Issue from '../../models/issue';

Then use it directly: Issue.create();


#3

You could also have used this.store.createRecord('issue'); without having to import the Issue model.


#4

Ok, I thought I tried that but that it did not work if I didn’t use ember-data. I will test it again :slight_smile: Thanks.


#5

Nope, you’re right. That won’t work if you’re not also using Ember Data. Sorry, I should not have assumed that you were.