Skip to content

Instantly share code, notes, and snippets.

@sunocean-sand
Last active August 29, 2015 14:20
Show Gist options
  • Save sunocean-sand/f45770cd563d539b585c to your computer and use it in GitHub Desktop.
Save sunocean-sand/f45770cd563d539b585c to your computer and use it in GitHub Desktop.
trying to show the post creator's name on the list
import Ember from 'ember';
import Firebase from 'firebase';
function parseAuthData(authData) {
var parsedData = {};
switch(authData.provider) {
case 'facebook':
parsedData.provider = authData.provider;
parsedData.id = authData.facebook.id;
parsedData.displayName = authData.facebook.displayName;
parsedData.gender = authData.facebook.cachedUserProfile.gender;
parsedData.language = authData.facebook.cachedUserProfile.locale;
parsedData.imageThumbUrl = authData.facebook.cachedUserProfile.picture.data.url;
parsedData.website = authData.facebook.cachedUserProfile.link;
return parsedData;
/*
case 'github':
parsedData.provider = authData.provider;
parsedData.id = authData.github.id;
parsedData.username = authData.github.username;
parsedData.displayName = authData.github.displayName;
parsedData.description = authData.github.cachedUserProfile.bio;
parsedData.email = authData.github.email;
parsedData.company = authData.github.cachedUserProfile.company;
parsedData.location = authData.github.cachedUserProfile.location;
parsedData.imageThumbUrl = authData.github.cachedUserProfile.avatar_url;
parsedData.website = authData.github.cachedUserProfile.html_url;
return parsedData;
case 'google':
parsedData.provider = authData.provider;
parsedData.id = authData.google.id;
parsedData.displayName = authData.google.displayName;
parsedData.gender = authData.google.cachedUserProfile.gender;
parsedData.language = authData.google.cachedUserProfile.locale;
parsedData.imageThumbUrl = authData.google.cachedUserProfile.picture;
parsedData.website = authData.google.cachedUserProfile.link;
return parsedData;
*/
case 'twitter':
parsedData.provider = authData.provider;
parsedData.id = authData.twitter.id;
parsedData.username = authData.twitter.username;
parsedData.displayName = authData.twitter.displayName;
parsedData.description = authData.twitter.cachedUserProfile.description;
parsedData.location = authData.twitter.cachedUserProfile.location;
parsedData.language = authData.twitter.cachedUserProfile.lang;
parsedData.imageThumbUrl = authData.twitter.cachedUserProfile.profile_image_url_https || authData.twitter.cachedUserProfile.profile_image_url;
parsedData.website = authData.twitter.cachedUserProfile.url;
return parsedData;
}
}
var session = Ember.Object.extend({
ref : new Firebase("https://nutella.firebaseio.com"),
addFirebaseCallback: function() {
var session = this;
var ref = this.get('ref');
ref.onAuth(function(authData) {
if (authData) {
var user = parseAuthData(authData);
session.set("isAuthenticated", true);
session.set('uid', authData.uid);
session.set('user', user);
ref.child('users').child(authData.uid).set(user);
} else {
session.set("isAuthenticated", false);
}
});
}.on("init"),
loginFacebook: function() {
var session = this;
return new Ember.RSVP.Promise(function (resolve, reject) {
session.get("ref").authWithOAuthPopup("facebook", function(error, user) {
if (user) {
resolve(user);
} else {
reject(error);
}
},
{
remember: "sessionOnly",
scope: "email"
});
});
},
loginTwitter: function() {
var session = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
session.get('ref').authWithOAuthPopup('twitter', function(error, user) {
if (user) { resolve(user); }
else { reject(error); }
});
});
},
login: function() {
return new Ember.RSVP.Promise((resolve, reject) => {
this.get('ref').authWithPassword({
email: "",
password: ""
}, function(error, user) {
if (user) { resolve(user); }
else { reject(error); }
});
});
},
createUser: function() {
var session = this;
var properties = {
name: this.get('name'),
email: this.get('email'),
password: this.get('password')
}
this.get('ref').createUser(properties, function(error, user) {
if (!error) {
console.log('User name: ' + user.name + ', Email: ' + user.email);
var userRef = new Firebase(usersPath + '/simplelogin:' + user.id);
userRef.set({email: user.email}); // This is where you'd add Display Name, phone #, whatever
session.login('password', email, password);
}
}
)},
logout: function() {
this.get("ref").unauth();
},
currentUser: Ember.computed('isAuthenticated', function() {
return this.get('ref').getAuth();
})
});
export default {
name: "Session",
initialize: function (container, app) {
app.register("session:main", session);
app.inject("controller", "session", "session:main");
app.inject("route", "session", "session:main");
}
};
<div class="row">
<div class="col-xs-12 center-block text-center">
{{#if isEditing}}
{{edit-title type="text" value=model.title focus-out="updateTitle" insert-newline="updateTitle"}}
{{else}}
<h4>{{model.title}}</h4>
<a href="#" {{action "editTitle"}}>{{fa-icon "pencil"}}</a>
<a href="#" {{action "deleteList"}}>{{fa-icon "remove"}}</a>
{{/if}}
</div>
</div>
<div class="row">
{{outlet "todos"}}
</div>
//models/list.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
todos: DS.hasMany('todo', {async: true}),
user: DS.belongsTo('user', {async: true})
});
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 text-center">
<h1>Hi {{session.user.displayName}},</h1>
<img src={{session.user.imageThumbUrl}} height="100" width="100" class="img-circle">
<p class="lead">what do you want to make happen today?</p>
<div class="input-group input-group-lg center-block">
{{input class="form-control" type="text" value=newListTitle action="createList" placeholder="Create a Stack"}}
</div>
<br><br><br><br>
<h4>Discover stacks in the works</h4>
</div>
</div>
<div class="row">
{{#each list in model}}
<div class="col-lg-4 col-sm-4 col-xs-12">
{{#link-to "todo" list}}
<div class="panel panel-default">
<div class="panel-body">
<li>{{list.title}}</li>
<li>{{list.currentUser.displayName}}</li>
{{!number of hands}}
<br>
<p>
<span class="badge pull-left">14 hands</span>
</p>
</div>
</div>
{{/link-to}}
</div>
{{/each}}
</div>
</div>
<!--div class="small-6 columns">
{{outlet}}
</div-->
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('list');
},
actions: {
createList: function() {
var newListTitle = this.controllerFor('lists').get('newListTitle');
var user = this.get('session.user.displayName');
alert(this.get('session.user.displayName'));
if (Ember.isBlank(newListTitle)) { return false; }
//1
var list = this.store.createRecord('list', {
title: newListTitle,
user: user,
});
console.log(user.get('lists'));
//2
this.controllerFor('lists').set('newListTitle', '');
/*
this.controllerFor('lists').setProperties({
title: newListTitle,
user: username,
});
*/
var _this = this;
console.log(user.get('lists'));
//3
list.save().then(function(list) {
user.get('lists').addObject(list);
user.save();
//_this.transitionTo('lists.show', list); //4
});
}
}
});
import DS from 'ember-data';
export default DS.Model.extend({
displayName: DS.attr('string'),
email: DS.attr('string'),
list: DS.hasMany('list', {async: true}),
todo: DS.hasMany('todo', {async: true})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment