简体   繁体   中英

Updating to 1.11.1 - View not accessible inside an helper

We are in the process of updating our ember app from 1.10 to 1.11 and we are running into an issue. We have a simple sort-toggle helper that is not working anymore.

{{#link-to "foo" (sort-toggle "id")}}

Code of the helper:

Ember.HTMLBars._registerHelper('sort-toggle', function(params, hash, options, env) {
    var target = this.get('controller.sortTarget') || hash.target,
        view = env.data.view,
        field = params[0];

    hash = {};
    hash[target] = view.getStream(this.get('controller').createToggleString(field));

    options = {
        hash: hash,
        data: options.data
    };

    return Ember.Handlebars.helpers['query-params'].helperFunction.call(this, [], hash, options, env);
});

It appears that since we updated to ember-cli 0.2.3 (ember 1.11.1), this is now undefined and so we cannot access the view or the controller.

We did not see anything in the changelog related to this, what changed ? and how should we migrate this ?

If I remember correctly, the 1.10 -> 1.11 upgrade was where a client of mine was blocked for a while as well. 1.11 and 1.12 didn't have good support for helpers and when they landed an updated version of helpers in 1.13 they worked quite a bit differently than they had before.

More details available here:

  1. https://github.com/emberjs/ember.js/issues/11080
  2. https://github.com/emberjs/rfcs/blob/master/text/0053-helpers.md
  3. https://github.com/mixonic/rfcs/blob/helper-listing/active/0000-helper-listing.md
  4. New helpers are also mentioned in the 1.13 blog post here: http://emberjs.com/blog/2015/06/12/ember-1-13-0-released.html

For my client, we had to make the jump to 1.13.3 (I believe) as both ember-can and ember-i18n were affected by those changes and we relied on them heavily.

Hope that helps!

So as @acorncom mentionned, the problem is that we were using a private API: _registerHelper .

We managed to get away with this for now since the view is still accessible in env.data.view and then the controller is just one get() away but this is only a temporary solution and we will have to refactor it soon.

The new code:

Ember.HTMLBars._registerHelper('sort-toggle', function(params, hash, options, env) {
    var view = env.data.view,
        field = params[0],
    target = view.get('controller.sortTarget') || hash.target;

    hash = {};
  var toggleString = view.get('controller').createToggleString(field);
    hash[target] = view.getStream(toggleString);

    options = {
        hash: hash,
        data: options.data
    };

    return Ember.Handlebars.helpers['query-params'].helperFunction.call(this, [], hash, options, env);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM