Skip to content

Instantly share code, notes, and snippets.

@dlferro
Created October 13, 2014 15:56
Show Gist options
  • Save dlferro/b84b13daa4e84cc1ebcd to your computer and use it in GitHub Desktop.
Save dlferro/b84b13daa4e84cc1ebcd to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript">
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://127.0.0.1:8000/api/content',
headers: function() {
return {'X-CSRFToken': $('meta[name="csrf-token"]').attr('content')};
}.property("session.authToken")
});
App = Em.Application.create({
rootElement: '#main-content'
});
App.Router.map(function() {
this.resource('socialItems', {path: '/'});
})
App.SocialItemsRoute = Ember.Route.extend({
model: function() {
return this.store.find('socialItem', {state: 0});
}
});
App.IsodateTransform = DS.Transform.extend({
deserialize: function (serialized) {
if (serialized) {
return moment(serialized).toDate();
}
return serialized;
},
serialize: function (deserialized) {
if (deserialized) {
return moment(deserialized).format();
}
return deserialized;
}
});
App.SocialItem = DS.Model.extend({
channel: DS.attr('string'),
state: DS.attr('number'),
text: DS.attr('string'),
image: DS.attr('string'),
video: DS.attr('string'),
media_caption: DS.attr('string'),
user: DS.belongsTo('socialUser', {async: true}),
created_at: DS.attr('isodate')
});
App.SocialUser = DS.Model.extend({
username: DS.attr('string'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
image: DS.attr('string'),
social_item_user: DS.hasMany('socialItem', {async: true})
});
App.SocialItemController = Em.ObjectController.extend({
actions: {
approveSocialItem: function() {
this.updateSocialItem(2);
},
deleteSocialItem: function() {
this.updateSocialItem(1);
}
},
updateSocialItem: function(state) {
var model = this.get('model'),
store = this.store;
model.set('state', state).save().then(function() {
store.unloadRecord(model);
store.find('socialItem', {count: 25, state: 0});
});
},
name: function() {
var model = this.get('model'),
username = model.get('user').get('username'),
fullName = model.get('user').get('first_name') + ' ' + model.get('user').get('last_name');
if (_.isEmpty(username)) {
return fullName;
} else if (!_.isEmpty(fullName)) {
return username;
} else {
return '';
}
}.property('model.user.username', 'model.user.first_name', 'model.user.last_name'),
hasMedia: function() {
return this.get('model.image') || this.get('model.video');
}.property('model.image', 'model.video')
});
App.SocialItemsController = Em.ArrayController.extend({
itemController: 'socialItem',
init: function() {
var that = this,
store = this.store;
setInterval(function() {
store.find('socialItem', {state: 0});
}, 5000);
this._super();
}
});
</script>
</head>
<body>
<div id="main-content"></div>
<script type="text/x-handlebars" data-template-name="socialItems">
<ul>
{{#each}}
<li>
<img {{bind-attr src=user.image}}>
</li>
{{/each}}
</ul>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment