supahken
supahken

Reputation: 161

EmberJS / ember-cli - how to not refer to model in the template?

I'm pretty new to EmberJS. Trying to make some simple apps to get more familiar with it. There's a behavior that I'm not quite understanding. I'm building this on ember-cli.

router

Router.map(function() {
  this.resource('quotes', {path: '/'}, function() {
    this.route('new');
    this.resource('quote', {path: '/quotes/:id'}, function() {
      this.route('delete');
    });
  });
});

routes/quote.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('quote', params.id);
  }
});

controllers/quote.js

import Ember from 'ember';

export default Ember.Controller.extend({

  actions: {

    deleteQuote: function() {
      // is calling this.model here OK?
      this.model.destroyRecord();
      this.transitionTo('quotes');
    }
  }

});

quotes.hbs

{{#link-to 'quotes.new' tagName='h3' class='new-quote'}}New Quote{{/link-to}}

<ul>
{{#each quote in controller}}
  {{#link-to 'quote' quote tagName='li' class='quotes'}}
    {{quote.title}}
  {{/link-to}}
{{/each}}
</ul>

{{outlet}}

quote.hbs

{{!-- probably shouldn't be referring to model directly here... --}}
<p class='description'>{{model.body}}</p> 

<button {{action deleteQuote}}>Delete Quote</button>

I only have to use 'model.body' in the quote template when I explicitly define a QuoteController. However, if I don't make a controllers/quote.js file, I can use body in the quote template without the model prefix.

I'm not sure if this is standard in Ember, but somehow it seems off to me that I'm calling model.something in the template. Would be grateful if someone could explain this logic/behavior to me and what's the "right" way to do this. Thanks a lot in advance!

[EDIT]

Thanks BenjaminRH for the solution. For anyone else looking at the question and want more info, here's something that may help: http://coryforsyth.com/2014/02/17/ember-controller-versus-objectcontroller/

Upvotes: 2

Views: 159

Answers (1)

BenjaminRH
BenjaminRH

Reputation: 12172

Try making it an ObjectController instead of a Controller. Here's the Ember guide for more detail.

Upvotes: 1

Related Questions