OSDN Git Service

fix event names
[pettanr/pettanr.git] / app / assets / javascripts / models / panel.js.coffee
index 2d90085..834bf3f 100644 (file)
@@ -1,17 +1,5 @@
 class Pettanr.Panel extends Peta.Root\r
   url: '/panels/'\r
-  relations: [\r
-    {\r
-      type: Backbone.HasMany,\r
-      key: 'panel_pictures',\r
-      relatedModel: 'Pettanr.PanelPicture',\r
-      collectionType: 'Pettanr.PanelPictures',\r
-      reverseRelation: {\r
-        key: 'panel',\r
-        includeInJSON: 'id'\r
-      }\r
-    },\r
-  ]\r
   \r
   @singular: () ->\r
     'Panel'\r
@@ -25,9 +13,20 @@ class Pettanr.Panel extends Peta.Root
     publish: 0\r
   } \r
   \r
+  author: () ->\r
+    new Pettanr.Author({id: @get('author_id')})\r
+  \r
   pp: () ->\r
     new Pettanr.PanelPicture({id: @get('pp_id')})\r
   \r
+  with_elements: () ->\r
+    new Pettanr.Panel.WithElements({id: @get('id')})\r
+  \r
+  overwrite: (options) ->\r
+    operators = options.operators\r
+    return false if not operators.author\r
+    @set('author_id', operators.author.id)\r
+  \r
   is_visible: (operators) ->\r
     switch super(operators)\r
       when null # super return\r
@@ -38,33 +37,57 @@ class Pettanr.Panel extends Peta.Root
         return true if @isNew()\r
         @is_publish()\r
   \r
+  overwrite: (options) ->\r
+    @set('author_id', options.operators.author.id)\r
+  \r
   is_usable: (operators) ->\r
     @is_visible(operators)\r
   \r
   is_publish: () ->\r
     @get('publish') > 0\r
   \r
+  @licensed_pictures: (panels) ->\r
+    r = {}\r
+    _.each panels, (panel) ->\r
+      _.extend r, panel.licensed_pictures()\r
+    r\r
+  \r
+  licensed_pictures: () ->\r
+    r = {}\r
+    _.each @scenario_elements(), (elm) ->\r
+      return if not elm.my_class().has_picture()\r
+      pid = elm.get('picture_id')\r
+      r[pid] = elm.picture() if not r[pid]\r
+    r\r
+  \r
+  my_url: () ->\r
+    '/' + @path_name() + '/' + @get('id')\r
+  \r
   initialize: (attr = {}, options = {}) ->\r
     @set(attr)\r
     if @get('id')\r
-      @url = @url + @get('id')\r
-    if options.with_elements\r
-      @url = @url + '?with_elements=true'\r
+      @url = @my_url()\r
+    @bind('change:width', () ->\r
+      console.log('cng')\r
+    )\r
   \r
-class Pettanr.Panels extends Backbone.Collection\r
-  model: Pettanr.Panel\r
-  url: '/panels/'\r
-class Pettanr.RelationPanel extends Pettanr.Panel\r
-  relations: [\r
-    {\r
-      type: Backbone.HasMany,\r
-      key: 'panel_pictures',\r
-      relatedModel: 'Pettanr.PanelPicture',\r
-      collectionType: 'Pettanr.PanelPictures',\r
-      reverseRelation: {\r
-        key: 'panel',\r
-        includeInJSON: 'id'\r
-      }\r
-    },\r
-  ]\r
+class Pettanr.Panel.WithElements extends Pettanr.Panel\r
+  \r
+  fetch: (options) ->\r
+    @url = @my_url() + '?with_elements=true'\r
+    super(options)\r
+  \r
+  save: (attrs, options = {}) ->\r
+    @url = @my_url()\r
+    options.attrs = {panel: attrs}\r
+    method = if @isNew()\r
+      'create'\r
+    else\r
+      'update'\r
+    xhr = @sync(method, this, options)\r
+    xhr\r
+  \r
+  destroy: (options) ->\r
+    @url = @my_url()\r
+    super(options)\r
   \r