Last active
March 8, 2017 23:44
-
-
Save poteto/93c638edfbea9b8bd948d881edb64d20 to your computer and use it in GitHub Desktop.
deeply set an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import deeplySet from '../utils/deeply-set'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
hierarchy: {}, | |
init() { | |
this._super(...arguments); | |
deeplySet(this.get('hierarchy'), 'company.region.department.employee.name', 'Jim Bob'); | |
deeplySet(this.get('hierarchy'), 'company.region.department.employee.age', 25); | |
deeplySet(this.get('hierarchy'), 'company.region.department.name', 'Accounting'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.11.1", | |
"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.11.0", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.11.0", | |
"ember-testing": "2.11.0" | |
}, | |
"addons": {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see https://gist.github.com/poteto/c32e1e48b63da58d64225b861389b709 | |
import Ember from 'ember'; | |
const { set, get } = Ember; | |
export default function deeplySet(obj, key, value) { | |
let keys = key.split('.'); | |
let prev; | |
while (keys.length > 1) { | |
let next = keys.shift(); | |
if (prev) { | |
next = `${prev}.${next}`; | |
} | |
set(obj, next, get(obj, next) || {}); | |
prev = next; | |
} | |
set(obj, key, value); | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment