Last active
May 31, 2017 22:56
-
-
Save sudowork/df7fa30d2eb3d32efe16185f4f376128 to your computer and use it in GitHub Desktop.
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'; | |
const { | |
computed, | |
get, | |
} = Ember; | |
export default Ember.Controller.extend({ | |
todos: computed('model', 'model.todos.[]', function() { | |
return get(this, 'model.todos'); | |
}), | |
hasTodos: computed('todos.[]', function() { | |
return get(this, 'todos').length > 0; | |
}), | |
actions: { | |
addTodo(todo) { | |
const todos = get(this, 'todos'); | |
todos.createRecord({ | |
name: todo, | |
}); | |
return false; | |
}, | |
removeTodo(todo) { | |
const todos = get(this, 'todos'); | |
console.log('todos:beforeRemoveObject', todos.toArray()); | |
todos.removeObject(todo); | |
console.log('todos:afterRemoveObject', todos.toArray()); | |
this.store.unloadRecord(todo); | |
console.log('todos:afterUnloadRecord', todos.toArray()); | |
// Re-get todos to demonstrate the issue | |
const newTodos = get(this, 'model.todos'); | |
// Should fail before we get here | |
console.log('newTodos:afterUnloadRecord', newTodos.toArray()); | |
return false; | |
}, | |
}, | |
}); |
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({ | |
todos: hasMany('todo', {async: false}) | |
}); |
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({ | |
name: attr('string') | |
}); |
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 config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: config.locationType, | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('todo-list', { path: '/' }); | |
}); | |
export default Router; |
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'; | |
const DUMMY_TODO_LIST = { | |
data: { | |
type: 'todo-list', | |
id: 42, | |
attributes: {}, | |
relationships: { | |
todos: { | |
data: [{ | |
id: '123', | |
type: 'todo', | |
}], | |
}, | |
}, | |
}, | |
included: [{ | |
type: 'todo', | |
id: 123, | |
attributes: { | |
name: 'Figure out this bug', | |
}, | |
}], | |
}; | |
export default Ember.Route.extend({ | |
model() { | |
this.store.pushPayload(DUMMY_TODO_LIST); | |
return this.store.peekRecord('todo-list', '42'); | |
}, | |
// Creating the record this way, however, works just fine | |
// model() { | |
// return this.store.createRecord('todo', { | |
// id: '42', | |
// todos: [], | |
// }); | |
// }, | |
// afterModel(model) { | |
// const todos = model.get('todos'); | |
// return todos.createRecord({ id: '123', name: 'this works though' }); | |
// }, | |
}); |
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.13.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment