Last active
November 21, 2016 12:39
-
-
Save HenryVonfire/d5796dcf0b18207e352c7cbe9b51a3c6 to your computer and use it in GitHub Desktop.
many to many problem
This file contains hidden or 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 hidden or 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({ | |
draft: Ember.inject.service(), | |
addresses: Ember.computed.mapBy('model.addresses', 'address'), | |
actions:{ | |
delete(address){ | |
const id = this.get('model.id'); | |
const draft = this.get('draft.records'); | |
if(!this.get(`draft.records.${this.get('model.id')}`)){ | |
draft[id] = []; | |
} | |
address.then(record => { | |
record.get('user').then(relationship => { | |
draft[id].pushObject({ | |
modelName: 'user-to-address', | |
model:relationship | |
}); | |
this.get('model.addresses').removeObject(relationship); | |
relationship.deleteRecord(); | |
}); | |
draft[id].pushObject({ | |
modelName: 'address', | |
model:record | |
}); | |
record.deleteRecord(); | |
}); | |
}, | |
undo(){ | |
const id = this.get('model.id'); | |
const draft = this.get('draft.records'); | |
draft[id].forEach(obj => { | |
obj.model.rollbackAttributes(); | |
if(obj.modelName === 'user-to-address'){ | |
this.get('model.addresses').pushObject(obj.model); | |
} | |
}); | |
this.set(`draft.records.${id}`,[]); | |
} | |
} | |
}); |
This file contains hidden or 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
export default function() { | |
this.get('/users', (schema) => { | |
return { | |
data: [ | |
{id:"1", type: "user", attributes: { name: "User 1"}}, | |
{id:"2", type: "user", attributes: { name: "User 2"}} | |
] | |
}; | |
}); | |
this.get('/users/:id',(schema) => { | |
return { | |
data: { | |
id: "1", | |
type:"user", | |
attributes: { name: "User 1" }, | |
relationships:{ addresses: { data: [{id:"1", type:"user-to-address"}] } } | |
}, | |
included: [ | |
{ | |
id: "1", | |
type: "user-to-address", | |
attributes: { user_id: "1", address_id:"1" }, | |
relationships: { | |
user:{ data: {id:"1", type:"user"} }, | |
address: { data: {id:"1", type: "address"} } | |
} | |
}, | |
{ | |
id: "1", | |
type: "address", | |
attributes: { street: "Street 1" }, | |
relationships: { | |
user: { data: {id: "1", type: "user-to-address" }} | |
} | |
} | |
] | |
}; | |
}); | |
}; |
This file contains hidden or 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 Mirage from 'ember-cli-mirage'; | |
export default Mirage.Factory.extend({ | |
street: function(i) { | |
return 'street ' + i | |
} | |
}); |
This file contains hidden or 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 Mirage from 'ember-cli-mirage'; | |
export default Mirage.Factory.extend({ | |
name: function(i) { | |
return 'User ' + i | |
} | |
}); |
This file contains hidden or 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
export default function(server) { | |
window.server = server; | |
// added this line because global wasn't created otherwise. | |
server.createList('user', 10); | |
server.createList('address', 10); | |
} |
This file contains hidden or 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({ | |
user: belongsTo('user-to-address'), | |
street: attr('string') | |
}); |
This file contains hidden or 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({ | |
address: belongsTo('address'), | |
user: belongsTo('user'), | |
userId: attr('string'), | |
addressId: attr('string') | |
}); |
This file contains hidden or 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({ | |
addresses: hasMany('user-to-address'), | |
name: attr('string') | |
}); |
This file contains hidden or 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 config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('users', function(){ | |
this.route('user', {path:':id'}); | |
}); | |
}); | |
export default Router; |
This file contains hidden or 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({ | |
beforeModel(){ | |
/*const store = this.get('store'); | |
let user = store.createRecord('user',{ | |
id: '1', | |
name: 'User 1' | |
}); | |
let address = store.createRecord('address',{ | |
id: '1', | |
street: 'street 1' | |
}); | |
let relationship = store.createRecord('user-to-address',{ | |
userId: '1', | |
addressId: '1' | |
}); | |
user.get('addresses').pushObject(relationship); | |
address.set('user', relationship); | |
relationship.set('user',user); | |
relationship.set('address',address);*/ | |
} | |
}); |
This file contains hidden or 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.get('store').findAll('user'); | |
} | |
}); |
This file contains hidden or 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({ | |
draft: Ember.inject.service(), | |
model(params){ | |
const draft = this.get(`draft.records${params.id}`); | |
if(!draft || draft.length === 0){ | |
return this.get('store').findRecord('user', params.id); | |
} | |
return this.get('store').peekRecord('user', params.id); | |
} | |
}); |
This file contains hidden or 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.Service.extend({ | |
records:[] | |
}); |
This file contains hidden or 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.10.6", | |
"ENV": { | |
"ember-cli-mirage": { | |
"enabled": true, | |
"directory": "app/mirage" | |
} | |
}, | |
"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.9.0", | |
"ember-data": "2.9.0", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": { | |
"ember-cli-mirage": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment