Skip to content

Instantly share code, notes, and snippets.

@tarponjargon
Last active February 19, 2017 21:13
Show Gist options
  • Select an option

  • Save tarponjargon/fb1680bfea201aeb4a0d886aafa3eed5 to your computer and use it in GitHub Desktop.

Select an option

Save tarponjargon/fb1680bfea201aeb4a0d886aafa3eed5 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
// this would not be here, I'd import it to both the route and the controller (perhaps from environment.js)
const cfg = {
defaultQueryParams: {
q: null,
sort_method: 'relevance', //<--when added with setProperties in init() - strings are added just fine
filter1: [], // <--when added with setProperties in init() - arrays are added as 'undefined'
filter2: []
}
};
export default Ember.Controller.extend({
queryParams: Object.keys(cfg.defaultQueryParams), // <--this works just fine, no need to manually specify
init(){
this._super(...arguments);
this.setProperties(cfg.defaultQueryParams); // <--works for setting strings, but not arrays
},
filter2: [], // <--when added directly, arrays are added as 'Array[0]'
actions: {
addFilter: function(key, value) {
alert(`adding "${value.toString()}" to controller property '${key}'`);
console.log("DUMP OF EMBER 'this':", this);
this[key].pushObject(value.toString());
}
}
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('results');
});
export default Router;
import Ember from 'ember';
// this would not be here, I'd import it to both the route and the controller (perhaps from environment.js)
const cfg = {
defaultQueryParams: {
q: null,
sort_method: 'relevance',
filter1: [],
filter2: []
}
};
export default Ember.Route.extend({
queryParams: {},
init() {
this._super(...arguments);
//specify from cfg.defaultQueryParams rather than each one explicitly
Object.keys(cfg.defaultQueryParams).forEach((qp) => {
this.queryParams[qp] = { refreshModel: true };
});
// setting each queryParams `refreshModel: true` using a loop does work! yay! See console:
console.log("ROUTE queryParams:", this.queryParams);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{link-to "Click here (unless you already did)" "results" (query-params q="foo")}}
{{outlet}}
<br>
<br>
<h1>Add a filter:</h1>
<br>
<a href="#" {{action "addFilter" "filter1" "Value of Filter 1"}}>Click to add a filter to filter1 array set in controller init()</a> (then see error in console and "DUMP OF EMBER 'this'" in console - note difference between default values of filter1 and filter2 arrays)
<br><br>
<a href="#" {{action "addFilter" "filter2" "Value of Filter 2"}}>Click to add a filter to filter2 array set right on the controller</a> (should work)
<br><br>
<h1>Selected filters (should be the result of clicking a link above):</h1>
Filter1: {{#each filter1 as |bar|}}{{bar}}{{/each}}<br>
Filter2: {{#each filter2 as |baz|}}{{baz}}{{/each}}<br>
{{outlet}}
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment