OSDN Git Service

fix: show's destroy button
[pettanr/pettanr.git] / app / assets / javascripts / models / user.js.coffee
index 131c2e8..2b2d1c5 100644 (file)
@@ -1,39 +1,41 @@
 class Pettanr.User extends Backbone.Model\r
   \r
-  url: '/users/'\r
-  \r
   defaults: {\r
-    id: null\r
   } \r
   \r
   author: () ->\r
   \r
   artist: () ->\r
   \r
-  initialize: () ->\r
+  initialize: (attr = {}, options = {}) ->\r
+    super(attr, options)\r
+    _.extend(this, Backbone.Events)\r
     if @id\r
       @url = @url + @id\r
   \r
-class Pettanr.User.Current extends Pettanr.User\r
+class Pettanr.User.Session extends Pettanr.User\r
+  \r
+  sign_in: () ->\r
+    @save({})\r
   \r
-  save: (model_attr, after_save = null) ->\r
-    _this = this\r
+  save: (model_attr) ->\r
+    @url = '/users/sign_in'\r
     super(model_attr, {\r
-      success: (model, response, options) ->\r
-        author = new Pettanr.Author(_this.get('author')) if _this.get('author')\r
-        artist = new Pettanr.Artist(_this.get('artist')) if _this.get('artist')\r
-        window.operators = new Pettanr.Operator({\r
-          user: _this, \r
-          author: author, \r
-          artist: artist\r
-        })\r
-        if after_save\r
-          after_save(_this)\r
-      error: (model, response, options) ->\r
-        r = new Pettanr.Views.User.Index({})\r
-        $("#pettanr").html(r.render().el)\r
+      success: (model, response, options) =>\r
+        @trigger('sign_in:success', model, response)\r
+      error: (model, response, options) =>\r
+        @trigger('sign_in:fail', model, response)\r
     })\r
   \r
-  initialize: () ->\r
-    @url = '/users/sign_in'\r
+  destroy: () ->\r
+    @url = '/users/sign_out'\r
+    @sync('delete', this, {\r
+      success: (model, response, options) =>\r
+        @trigger('sign_out:success', model, response)\r
+      error: (model, response, options) =>\r
+        @trigger('sign_out:fail', model, response)\r
+    })\r
+  \r
+  initialize: (attr = {}, options = {}) ->\r
+    super(attr, options)\r
   \r