lundi 24 août 2015

ember-cli data returned empty using initializer

I have an app where we need to create an initializer that inject our global into all the route where our global is a function that load data from a JSON file and return the data.

global-variable.js

export function initialize(container, application) {
    var systemSetting = {
        systemJSON: function(){
            return Ember.$.getJSON("system/system.json").then(function(data){
                return data
            });
        }.property()
    };
    application.register('systemSetting:main', systemSetting, {instantiate: false});
    application.inject('route', 'systemSetting', 'systemSetting:main');
}

export default {
  name: 'global-variable',
  initialize: initialize
};

index.js - route

export default Ember.Route.extend({
  activate: function(){
    var _settings = self.systemSetting.systemJSON;
    console.log(_settings.test);
  },
}

system.JSON

{
    "test" : 100
}

the result of the console.log give me this

ComputedProperty {isDescriptor: true, _dependentKeys: Array[0], _suspended: undefined, _meta: undefined, _cacheable: true…}

I think it's because of the JSON is not loaded yet but after that I try to do something like this at route

index.js - route

activate: function(){
    var self = this;
    var run = Ember.run

    run.later(function() {
        var _settings = self.systemSetting.systemJSON;
        console.log(_settings);
    }, 1000);
},

but still give me the same log. Am I use wrong approach to this problem?




Aucun commentaire:

Enregistrer un commentaire