xamenrax
xamenrax

Reputation: 1744

Ember.js setupController fired only once

Suppose I have a view:

CellarRails.SearchTextField = Ember.TextField.extend({
  templatename: 'index',
  insertNewline: function(){
    this.get('controller').set('query', this.get('value'));

    // calling search method of application controller
    this.get('controller').send('search');
    this.set('value', '');
  }
});

And an applicationController:

CellarRails.ApplicationController = Ember.Controller.extend({
  needs: ['search'],
  query: '',

  // here is the search method
  search: function() {

    // I'm using ember-query lib, which provides this helper, it acts just like usual transitionToRoute
    this.transitionToRouteWithParams('search', {
      q: this.get('computedQuery')
    });
  },
  computedQuery: function() {
    this.get('controllers.search').set('q', this.get('query'));
    return this.get('query');
  }.property('query')
});

So now it should be transitioned to searchRoute:

CellarRails.SearchRoute = Ember.Route.extend({
  serializeParams: function(controller) {
    return {
      q: controller.get('q')
    };
  },
  deserializeParams: function(params, controller) {
    controller.set('q', params.q);
  },

  // pass CellarRails.Track model to SearchController's context
  setupController: function(controller, context, params) {
    console.log('setup controller hooked!');
    controller.set('context', CellarRails.Track.find(params));
  }
});

In the CellarRails.Track model I have redefined a find method. PROBLEM: This code works, but setupController hook fires only for the first time (when I transition from applicationRoute to searchRoute), BUT if I already in searchRoute this hook doesn't fire and find method of CellarRails.Track model doesn't fire too.

Upvotes: 0

Views: 1769

Answers (3)

xamenrax
xamenrax

Reputation: 1744

Here is an explanation of this issue: https://github.com/alexspeller/ember-query/issues/9

Upvotes: 0

intuitivepixel
intuitivepixel

Reputation: 23322

When you setup setupController on your route the model hook is not invoked. If you want that both hooks model and setupController should fire you have to call this._super(...) in your setupController hook to maintain the model hook default behaviour:

CellarRails.SearchRoute = Ember.Route.extend({
  ...
  model: function(params) {
    return CellarRails.MyModel.find();
  },
  setupController: function(controller, model) {
    this._super(controller, model);
    ...
  }
  ...
});

Hope it helps.

Upvotes: 3

Patrick
Patrick

Reputation: 472

try using the model: hook in the SearchRoute

model: function ( params, transition ) {
    return CellarRails.Track.find(params);
}

This gets called if you navigate direct to the url, hopefully the required params will be in the params but try doing a debug on it to check :)

Upvotes: 0

Related Questions