Last active
March 27, 2018 00:05
-
-
Save Kilowhisky/099d510ed467e1621975 to your computer and use it in GitHub Desktop.
ArrayWillChange LiveRecordArray Issue
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 DS from 'ember-data'; | |
export default DS.RESTAdapter.extend({ | |
shouldBackgroundReloadRecord(store, snapshot){ | |
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 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'; | |
export default Ember.Route.extend({ | |
init: function(){ | |
this._super(...arguments); | |
$.mockjax({ | |
url: '/fields', | |
responseText: [ | |
{ id: 1, name: 'how', posts: [1] }, | |
{ id: 2, name: 'now', posts: [2,4] }, | |
{ id: 3, name: 'cow', posts: [3] } | |
] | |
}); | |
$.mockjax({ | |
url: '/posts', | |
responseText: [ | |
{ id: 1, name: 'how', utc: '2015-05-12T00:00:00' }, | |
{ id: 2, name: 'now', utc: '2015-05-12T00:05:00' }, | |
{ id: 3, name: 'brown', field: 3, utc: '2015-05-12T00:02:00' }, | |
{ id: 4, name: 'cow', utc: '2015-05-12T00:10:00' }, | |
{ id: 6, name: 'six', utc: '2015-05-12T00:35:00' } | |
] | |
}); | |
$.mockjax({ | |
url: '/posts/2', | |
responseText: { id: 2, name: 'now', utc: '2015-05-12T00:05:00' } | |
}); | |
$.mockjax({ | |
url: '/posts/6', | |
responseText: { id: 6, name: 'six', utc: '2015-05-12T00:35:00' } | |
}); | |
}, | |
afterModel: function(model){ | |
this.store.findAll('post'); | |
model.post.addArrayObserver(this, { | |
willChange: 'dependentArrayWillChange', | |
didChange: 'dependentArrayDidChange' | |
}); | |
}, | |
model: function(){ | |
return Ember.RSVP.hash({ | |
field: this.store.findAll('field'), | |
post: this.store.peekAll('post') | |
}); | |
}, | |
dependentArrayWillChange(observedArray, start, removeCount /*, addCount */ ){ | |
if(removeCount > 0){ | |
let removedSlice = observedArray.slice(start, start + removeCount); | |
console.log(start, removeCount, observedArray.get('length')); | |
console.log(observedArray.toArray()); | |
console.log(removedSlice); | |
alert('object removed is ' + removedSlice); | |
} | |
}, | |
dependentArrayDidChange(observedArray, start, removeCount, addCount){ | |
}, | |
actions: { | |
unload(){ | |
[2,6].forEach(x => { | |
let record = this.store.peekRecord('post', x); | |
if(record){ | |
this.store.unloadRecord(record); | |
} | |
}); | |
}, | |
load(type, id){ | |
this.store.findRecord(type, id); | |
} | |
} | |
}); |
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 DS from 'ember-data'; | |
import Ember from 'ember'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
posts: DS.hasMany('post', {async: false }), | |
postsSorted: Ember.computed.sort('posts','postsSortedBy'), | |
postsSortedBy: ['utc:asc'] | |
}); |
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 DS from 'ember-data'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
utc: DS.attr('date'), | |
field: DS.belongsTo('post', {async: true}) | |
}); |
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 DS from 'ember-data'; | |
export default DS.JSONSerializer.extend({}); | |
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.4.16", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.16.2", | |
"ember-data": "2.18.1", | |
"ember-template-compiler": "2.16.2", | |
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment