7644

Accessing a model from a controller in Ember

Question:

I have a pretty basic setup where I'm trying to format a date in my Controller. The problem is I can't access it in the formattedStart function below, whereas I CAN access it in the summaryRowAction handler. This is baffling me, because console.logging this in both places gives the same result. But for some reason inside of formattedStart, this.get('model.startDate') is undefined.

App.SummaryRowController = Ember.ObjectController.extend({ formattedStart: function(){ console.log(this.get('model.startDate');) return this.get('model.startDate'); }.property(), actions: { summaryRowAction: function(){ console.log(this.get('model.startDate')); } } });

Here is my model and my template (in Jade) for reference:

App.PricingSummary = DS.Model.extend({ startDate: DS.attr(), endDate: DS.attr(), days: DS.hasMany('day', {async: true}), property: DS.belongsTo('property', {async: true}) }); script(type="text/x-handlebars", data-template-name="summaryRow") .summaries__summary("{{action 'summaryRowAction'}}") .summaries__summary--item{{formattedStart}} — {{endDate}}

Answer1:

It's because the first (and only) time that the property is evaluated, model is actually null. You need to specify startDate as a dependency in the property so Ember knows to re-evaluate when the data changes. Also, you don't need model.* in an object controller, the properties are automatically delegated to content/model

So:

formattedStart: function(){ console.log(this.get('startDate');) return this.get('startDate'); }.property('startDate'),

Recommend

  • Ember-Model hasMany does not save model emberjs
  • A proper way to separate concerns?
  • How to return a PartialView from Core 2 RazorPage ViewModel Handler
  • How to re-render Reactjs component after update in data?
  • Universal Windows Platform App with google calendar
  • one month after start date-joda
  • chrome extension inject javascript at top of head before any other js
  • Apply seq.Date using two dataframe columns [duplicate]
  • Get working days in SQL Server
  • HTML select, correct option selected in DOM, but wrong item shown in firefox
  • Format MySQL using CONCAT
  • Looping SQL statement insert
  • how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsi
  • In an Ember view, what's the best way to run code after the rerendering of an internal child vi
  • Ember.js Acceptance Testing not waiting for asynchronous data operations
  • Java: List the dates between a start and end date [duplicate]
  • LINQ to NHibernate WHERE EXISTS IN
  • SharePoint Designer 2010 - Determine if today's date is within x days of a start date column us
  • Use query params of parent route to refresh the model in subroute
  • Can you build a truly RESTful service that takes many parameters?
  • Angular2 - Template reference inside NgSwitch
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Clear fused location provider's location for testing
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Debug.DrawLine not showing in the GameView
  • Yii2: Config params vs. const/define
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Is calc() supported in html email?
  • req.body is undefined - nodejs
  • Accessing IRQ description array within a module and displaying action names
  • Javascript Callbacks with Object constructor
  • Where to put my custom functions in Wordpress?
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?