vendredi 10 février 2017

Ember: add entire object to controller

Is there an easy way to add controller properties directly from a javascript object?

As opposed to having to manually specify each and every query param I want to use in 3 places: queryParams, QP controller properties, and route queryParams[key].refreshModel = true...I just want to maintain this stuff in ONE object and update it to all those places:

const cfg = {
    defaultQueryParams: {
        q: null,
        paging: '18',
        search_sort: 'relevance',
        search_title: null,
        search_page: null,
        filter_breadcrumb: [],
        filter_price: [],
        filter_size_apparel: [], 
        filter_color: [] 
    }
};

In the controller I tried:

export default Ember.Controller.extend({
    queryParams: Object.keys(cfg.defaultQueryParams), // works great!

    init() {  
        this._super(...arguments);
        this.addObject(cfg.defaultQueryParams); // doesn't work: "this.addObjects is not a function"
        // same with addObjects, setObjects
    },
    ...
});

I also tried iterating over the JS object and using the setter. It works for strings, but not for the empty arrays:

    for (let [key, value] of Object.entries(cfg.defaultQueryParams)) {  
        this.set(key, value);
    }

The arrays are added as properties but in the console are undefined rather than Array[0]

Am I way off base in wanting to do this, or wanting to do it this way?

Go easy! Ember newb! :D




Aucun commentaire:

Enregistrer un commentaire