Created
April 12, 2013 09:57
-
-
Save sbellity/5370952 to your computer and use it in GitHub Desktop.
Hull Profile Widget with data coming from Facebook
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
Hull.widget('profile', { | |
templates: ['profile'], | |
refreshEvents: ['model.hull.me.change'], | |
fields: [ | |
{ name: 'name', type: 'text', placeholder: 'Name' }, | |
{ name: 'email', type: 'text', placeholder: 'Email' } | |
], | |
options: { | |
fields: 'name,email' | |
}, | |
datasources: { | |
profile: function() { | |
if (this.loggedIn()) { | |
return this.api('hull/me/profile'); | |
} | |
}, | |
facebookData: function() { | |
if (this.loggedIn().facebook) { | |
return this.api('facebook/me', { fields: this.options.fields }); | |
} | |
} | |
}, | |
actions: { | |
save: function(source, event, data) { | |
event.preventDefault(); | |
var profileFields = this.$el.find('form').serializeArray(); | |
var profileData = {}; | |
_.each(profileFields, function(f) { profileData[f.name] = f.value; }); | |
this.api('hull/me/profile', profileData, 'put').then(_.bind(function() { | |
this.sandbox.emit('hull.profile.saved'); | |
this.render(); | |
}, this)); | |
} | |
}, | |
beforeRender: function(data) { | |
var fieldNames = _.pluck(this.fields, 'name'); | |
data.fields = _.clone(this.fields); | |
if (data.profile && this.loggedIn().facebook) { | |
data.profile = _.extend(data.facebookData, data.profile); | |
_.each(data.fields, function(f) { | |
f.value = data.profile[f.name]; | |
}); | |
} | |
return data; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment