Created
November 29, 2017 13:13
-
-
Save ankushdharkar/ed49888cad344d7b0cb7a1ef8765b2cd to your computer and use it in GitHub Desktop.
ED Example
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 $ from 'jquery'; | |
// import Promise from 'rsvp'; | |
import DS from 'ember-data'; | |
export default DS.Adapter.extend({ | |
findRecord(store, type, id, snapshot) { | |
return {'imgURL': 'New Img comes here'}; | |
}, | |
createRecord(store, type, snapshot){ | |
let data = this.serialize(snapshot, { includeId: true }); | |
data.id = 0; | |
return data; | |
}, | |
updateRecord(store, type, snapshot){ | |
}, | |
deleteRecord(store, type, snapshot){ | |
}, | |
findAll(store, type, sinceToken){ | |
}, | |
query(store, type, query){ | |
}, | |
// // optional, to increase speed | |
// findMany(store, type, ids, snapshots){ | |
// } | |
}); |
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 Data example', | |
init() { | |
// Assuming some other route had made this entry | |
this.get('store').createRecord('simple-data', { | |
id: 0, | |
imgURL: 'In the beginning there was nothing in the universe' | |
}); | |
// Record now exists | |
let x = findRecord with id 0; // How to fill this? | |
this.set('data', x); | |
console.clear(); | |
}, | |
actions: { | |
applyNewData() { | |
this.get('data').set('imgURL', 'Then there was a big bang!'); | |
this.get('store').findRecord('simple-data', 0).then((dt) => { | |
console.log(`%c ${dt.get('imgURL')}`, 'font-size: 100px; color:yellow;background:black'); // Look ma free two way binding! | |
}); | |
} | |
} | |
}); |
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({ | |
imgURL: 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 DS from 'ember-data'; | |
export default DS.Serializer.extend({ | |
serialize(snapshot, options){ | |
var json = this._super(...arguments); | |
return json; | |
}, | |
normalizeResponse(store, primaryModelClass, payload, id, requestType) { | |
var attrHash = {}; | |
attrHash['id'] = 0; // Self user | |
attrHash['imgURL'] = payload['imgURL']; | |
let JSONAPIData = this.normalize(primaryModelClass, attrHash); | |
return JSONAPIData; | |
}, | |
normalize(modelClass, resourceHash) { | |
var data = { | |
id: resourceHash.id, | |
type: modelClass.modelName, | |
attributes: resourceHash | |
}; | |
return { data: data }; | |
} | |
}); |
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.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