Created
September 15, 2017 05:16
-
-
Save mwpastore/d629c84fe7090b08e0ba9a25129a6039 to your computer and use it in GitHub Desktop.
New Twiddle
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'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
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 RSVP from 'rsvp' | |
const ObjectPromiseProxy = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin) | |
export default function(dependentKey, defaultValues={}) { | |
return Ember.computed(dependentKey, { | |
get() { | |
const promise = this.get(dependentKey).then(model => model || RSVP.reject()).catch((e) => { | |
const { type } = this.get('constructor.relationshipsByName').get(dependentKey) | |
const model = this.get('store').createRecord(type, defaultValues) | |
return this.set(dependentKey, model) | |
}) | |
return ObjectPromiseProxy.create({ promise }) | |
}, | |
set(_, model) { | |
return this.set(dependentKey, model) | |
} | |
}) | |
} |
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 Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
export default Model.extend({ | |
number: attr('string'), | |
customer: belongsTo() | |
}); |
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 Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
import belongsToWithDefault from '../macros/belongs-to-with-default'; | |
export default Model.extend({ | |
name: attr('string'), | |
creditCard: belongsTo(), | |
creditCardWithDefault: belongsToWithDefault('creditCard', { | |
number: '123456' | |
}) | |
}); |
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'; | |
export default Ember.Route.extend({ | |
model() { | |
return this.store.createRecord('customer', { | |
name: 'fooCorp' | |
}).get('creditCardWithDefault'); | |
} | |
}); |
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.12.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.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment