Skip to content

Instantly share code, notes, and snippets.

@nickpoorman
Created September 7, 2012 20:12
Show Gist options
  • Save nickpoorman/3669240 to your computer and use it in GitHub Desktop.
Save nickpoorman/3669240 to your computer and use it in GitHub Desktop.
Can't get attributes
define(["app",
// Libs
"backbone", "validator",
// Modules
// Plugins
"util", "plugins/jquery.cookie"
//"plugins/backbone.layoutmanager"
],
function (app, Backbone, Validator, Util, Cookie) {
// Create a new module
var User = app.module();
User.Model = Backbone.Model.extend({
// http://blog.opperator.com/post/15671431847/backbone-js-sessions-and-authentication
urlRoot: '/api/session',
idAttribute: "_id",
defaults: {
firstName: undefined,
lastName: undefined,
email: undefined,
_id: undefined
},
initialize: function () {
this.fetch({
success: function (model, res) {
console.log("====after fetch=====");
console.log(model);
console.log(model.toJSON());
console.log("//====after fetch=====");
},
error: function () {
console.log("ERROR CALLED in user.js!");
}
});
// {
// error: function(model, res){
// //TODO: I guess we have to catch this somewhere
// // else because jQuery is still spitting out the
// // error to the console
// // Just had and idea! Maybe instead of making backbone know if it is authed
// // we just catch the unauthorized and redirect backbone to the login
// // whenever it tries to acces a protected resource
// // ... I think the only edge case for this would be when we are trying to log in and we gen an unauthorized
// }
// }
},
isLoggedIn: function () {
console.log('ID IS-----');
console.log(this);
console.log(this.id);
console.log(this.toJSON());
console.log(this.get("id"));
console.log(this.get("_id"));
console.log(this.get("email"));
console.log('ID IS-----');
//return !this.isNew();
return Boolean(this.get("id")); // !!
},
// not currently using this
saveCookie: function (auth_hash) {
$.cookie('user_id', auth_hash.id);
$.cookie('access_token', auth_hash.access_token);
},
// not currently using this
loadCookie: function () {
this.set({
user_id: $.cookie('user_id'),
access_token: $.cookie('access_token')
});
}
});
// Required, return the module for AMD compliance
return User;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment