Last active
November 6, 2024 01:10
-
-
Save Bestra/9793cb1b7f5f4e16935f to your computer and use it in GitHub Desktop.
Cat Trader
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({ | |
websocket: Ember.inject.service(), | |
listener: Ember.inject.service('websocket-listener'), | |
appName:'Cat Trader', | |
users: Ember.computed.alias('model'), | |
payloads: [], | |
init() { | |
this._super(...arguments); | |
let listener = this.get('listener'); | |
listener.set('messageCallback', this.showPayloads.bind(this)); | |
listener.listen(); | |
}, | |
showPayloads(m) { | |
this.get('payloads').pushObject(m); | |
}, | |
actions: { | |
trade(currentUserIndex, cat, inc) { | |
let newUser = this.get('users').objectAt(currentUserIndex + inc); | |
if (!newUser) { return; }; | |
cat.set('user', newUser); | |
}, | |
rename(cat, newName) { | |
cat.set('name', newName); | |
}, | |
fakeWebsocket(fnName) { | |
this.get('websocket')[fnName](); | |
} | |
} | |
}); |
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 Factory from 'demo-app/factories/payload' | |
const catPayload = { | |
data: [ | |
Factory.user("1", "Batman", ["1"]), | |
Factory.user("2", "Robin", ["2"]) | |
], | |
included: [ | |
Factory.cat("1", "Fluffy", "1"), | |
Factory.cat("2", "Cuddles", "2") | |
] | |
} | |
export default Ember.Route.extend({ | |
beforeModel() { | |
this.store.push(catPayload); | |
}, | |
model() { | |
return this.store.peekAll('user'); | |
}, | |
setupController(controller, model) { | |
controller.set('model', model); | |
controller.set('catCount', model.get('length')); | |
} | |
}); |
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.Component.extend({ | |
cat: null, | |
newName: "", | |
actions: { | |
startRename() { | |
this.setProperties({renaming: true, newName: this.get('cat.name')}); | |
}, | |
confirmRename() { | |
this.set('renaming', false); | |
this.attrs.rename(this.get('newName')); | |
} | |
} | |
}); |
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
Title: Bad Stuff | |
Client->Server:Save Cat 3 | |
Websocket->Client: Cat 3 Updated | |
Note over Client: WTF? | |
Server->Client: Cat 3 Saved |
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
let user = function(id, name, catIds) { | |
let cats = catIds.map(function(i) { | |
return {type: "cat", id: id}; | |
}); | |
return { | |
type: "user", | |
id: id, | |
attributes: {name: name}, | |
relationships: { | |
cats: { | |
data: cats | |
} | |
} | |
}; | |
} | |
let cat = function(id, name, userId) { | |
return { | |
type: "cat", | |
id: id, | |
attributes: {name: name}, | |
relationships: { | |
user: { | |
data: {type: "user", id: userId} | |
} | |
} | |
}; | |
} | |
export default {user, cat}; |
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'), | |
user: DS.belongsTo('user', {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 DS from 'ember-data'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
cats: DS.hasMany('cat', {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 Ember from 'ember'; | |
import config from './config/environment'; | |
const Router = Ember.Router.extend({ | |
location: config.locationType | |
}); | |
Router.map(function() { | |
}); | |
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
{ | |
"version": "0.5.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js", | |
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.1/ember-data.js", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js", | |
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js" | |
} | |
} |
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.Service.extend({ | |
websocket: Ember.inject.service(), | |
store: Ember.inject.service(), | |
messageCallback: null, | |
init() { | |
this._super(...arguments); | |
}, | |
handleMessage(m) { | |
if (this.messageCallback) { | |
this.messageCallback(m); | |
} | |
let { type, data } = JSON.parse(m); | |
if (type === 'update') { | |
this.update(data); | |
} else if (type === 'delete') { | |
this.delete(data); | |
} | |
}, | |
update(data) { | |
console.log(`pushing ${data}`); | |
this.get('store').push({data}); | |
}, | |
delete({type, id}) { | |
let record = this.get('store').peekRecord(type, id); | |
console.log(`deleting ${record}`); | |
if (record) { this.get('store').unloadRecord(record); } | |
}, | |
listen() { | |
let fn = this.handleMessage.bind(this); | |
this.get('websocket').on('message', fn); | |
} | |
}); |
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 Factory from 'demo-app/factories/payload' | |
export default Ember.Service.extend(Ember.Evented, { | |
addPatches() { | |
let payload = { | |
type: 'update', | |
data: Factory.cat("3", "Patches", "1") | |
} | |
this.trigger('message', JSON.stringify(payload, null, 2)); | |
}, | |
renameFluffy() { | |
let payload = { | |
type: 'update', | |
data: Factory.cat("1", "Steve", "1") | |
} | |
this.trigger('message', JSON.stringify(payload, null, 2)); | |
}, | |
deleteCuddles() { | |
let payload = { | |
type: 'delete', | |
data: {type: "cat", id: "2"} | |
} | |
this.trigger('message', JSON.stringify(payload, null, 2)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment