Alexandre Mélard
Alexandre Mélard

Reputation: 12649

setting model from route upon action received in ember.js

I am trying to set a model value from an action received by my route.

//app/routes/map.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return {
      trail: null
    };
  },
  actions: {
    event: function(name, value) {
      if (name === 'trail.selected') {
        this.modelFor('map').set('trail', value);
      }
    }
  }
});

when I try to use

 this.modelFor('map').set('trail', value);

I get the following error:

Uncaught TypeError: this.modelFor(...).set is not a function

When I try to use

this.modelFor('map').trail = value;

I get that error

Uncaught Error: Assertion Failed: You must use Ember.set() to set the trail property (of [object Object]) to <nested-component@model:mtg-trail::ember617:kvdpo>.

EDIT Added template

//app/templates/map.hbs
<h2>Trail's name: {{trail.name}}</h2>
{{#if trail}}
  {{map-draw trail=trail.id}}
{{/if}}

Upvotes: 1

Views: 721

Answers (2)

Kori John Roys
Kori John Roys

Reputation: 2661

Well, since the action you are calling is on the 'map' route itself, why not just:

this.set('model.trail', value);

Upvotes: 0

Jakeii
Jakeii

Reputation: 1273

Your routes model isn't an ember object so set won't work. Try:

  model: function() {
    return Ember.Object.create({
      trail: null
    });
  },

Also, changing the models content from an action should really be done on the controller.

Upvotes: 3

Related Questions