OSDN Git Service

fix: fetch err
[pettanr/pettanr.git] / app / assets / javascripts / peta / item.js.coffee
index b919c37..f5fa53b 100644 (file)
@@ -91,6 +91,8 @@ class Peta.Item extends Backbone.Model
   @trace_routes: () ->\r
     {}\r
   \r
+  @default_label_shorten_length = 13\r
+  \r
   #InstanceMethods\r
   \r
   default_url: () ->\r
@@ -125,56 +127,55 @@ class Peta.Item extends Backbone.Model
     else\r
       @item_name()\r
   \r
+  _label: (label_column_name, options) ->\r
+    shorten = options.shorten\r
+    l = if _.isBoolean(shorten)\r
+      if shorten\r
+        # supply default shorten_length\r
+        @constructor.default_label_shorten_length\r
+      else\r
+        # False be no shorten\r
+        null\r
+    else\r
+      # numeric\r
+      shorten\r
+    Pettanr.truncate(@get(label_column_name), l)\r
+  \r
+  label: (options) ->\r
+    ''\r
+  \r
   get_association: (routes, context, options) ->\r
-    console.log 'get_association'\r
     routes = [routes] if _.isString(routes)\r
-    console.log routes\r
     route = routes.shift()\r
     if _.isEmpty(routes)\r
       # fetching terminate association. callback\r
-      console.log 'fetching terminate association'\r
       cxt = options.context || context\r
       @fetch_association(route, cxt, {\r
         success: (association_item, options) =>\r
-          console.log 'success'\r
-          console.log association_item\r
-          console.log options\r
           options.success.call(context, association_item)\r
         context: context,\r
         options: options\r
       })\r
     else\r
       # fetching through associations\r
-      console.log 'fetching through associations'\r
       @fetch_association(route, this, {\r
         success: (association_item, options) =>\r
-          console.log 'success'\r
-          console.log association_item\r
-          console.log options\r
           association_item.get_association(routes, this, options)\r
         context: context,\r
         options: options\r
       })\r
   \r
   fetch_association: (name, context, options) =>\r
-    console.log context\r
-    console.log options\r
     a = @my_class().my_manifest().associations\r
     fetch_options = {\r
       success: (association_item) =>\r
         options.success.call(context, association_item, options.options)\r
     }\r
     if a.belongs_to[name]\r
-      console.log 'belongs_to'\r
-      console.log name\r
       @get_parent(name, context, fetch_options)\r
     else if a.has_many[name]\r
-      console.log 'has_many'\r
-      console.log name\r
       @get_children(name, context, fetch_options)\r
     else if a.has_one[name]\r
-      console.log 'has_one'\r
-      console.log name\r
       @get_child(name, context, fetch_options)\r
     else\r
       console.error('association does not exist in model manifest')\r
@@ -186,6 +187,9 @@ class Peta.Item extends Backbone.Model
     @listenTo(retriever, 'retrieve', (item) =>\r
       options.success.call(context, item)\r
     )\r
+    @listenTo(retriever, 'fail', (response, opt) =>\r
+      options.fail.call(context, response, opt)\r
+    )\r
     retriever.retrieve()\r
   \r
   get_child: (has_one_name, context, options = null) ->\r
@@ -260,6 +264,10 @@ class Peta.Item extends Backbone.Model
       callback = options.success\r
       callback.call(context, item)\r
     )\r
+    @listenTo(retriever, 'fail', (response, opt) =>\r
+      callback = options.fail\r
+      callback.call(context, response, opt)\r
+    )\r
     retriever.retrieve(options.force)\r
   \r
   @pick_item_name: () ->\r
@@ -296,14 +304,88 @@ class Peta.Item extends Backbone.Model
   symbol_option: (context, options) ->\r
     @trace_to('symbol', this, {\r
       success: (symbol_item) => \r
-        options.success.call(context, symbol_item.to_symbol())\r
+        options.success.call(context, symbol_item.symbol_file())\r
     })\r
   \r
-  to_symbol: () ->\r
-    @tmb_opt_img_tag()\r
+  @face_file: () ->\r
+    new Pettanr.ImageFile('/images/' + @item_name() + '.gif')\r
+  \r
+  face_file: () ->\r
+    @my_class().face_file()\r
+  \r
+  # thumbnail size picture\r
+  symbol_file: (subdir) ->\r
+    new Pettanr.PictureFile(this, subdir)\r
+  \r
+  # real size picture\r
+  picture_file: (subdir = null) ->\r
+    new Pettanr.PictureFile(this, subdir)\r
+  \r
+  real_picture: (subdir = null) ->\r
+    new Pettanr.View.RealIcon(@picture_file(subdir))\r
+  \r
+  # item.face_button({\r
+  #   context: this, \r
+  #   click: () ->\r
+  #     # ...\r
+  # })\r
+  face_button: (options) ->\r
+    Pettanr.View.face_button(this, @face_file(), options)\r
+  \r
+  mini_face_button: (options) ->\r
+    Pettanr.View.mini_face_button(this, @face_file(), options)\r
   \r
-  icon_view: (half) ->\r
-    new Pettanr.Views[@my_class().singular()].Icon({item: this, half: half})\r
+  prof_button: (options) ->\r
+    Pettanr.View.prof_button(@prof_url(), options)\r
+  \r
+  mini_prof_button: (options) ->\r
+    Pettanr.View.mini_prof_button(@prof_url(), options)\r
+  \r
+  symbol_button: (options) ->\r
+    Pettanr.View.face_button(this, @symbol_file(), options)\r
+  \r
+  mini_symbol_button: (options) ->\r
+    Pettanr.View.mini_face_button(this, @symbol_file(), options)\r
+  \r
+  # pencil button\r
+  edit_button: (options) ->\r
+    icon = new Pettanr.View.Icon(Pettanr.View.Image.icon_edit_file())\r
+    Pettanr.View.any_button(this, @edit_url(), icon, options)\r
+  \r
+  mini_edit_button: (options) ->\r
+    icon = new Pettanr.View.Minicon(Pettanr.View.Image.icon_edit_file())\r
+    Pettanr.View.mini_any_button(this,  @edit_url(), icon, options)\r
+  \r
+  # x button\r
+  destroy_button: (options) ->\r
+    icon = new Pettanr.View.Icon(Pettanr.View.Image.icon_destroy_file())\r
+    Pettanr.View.any_button(this, @destroy_url(), icon, options)\r
+  \r
+  mini_destroy_button: (options) ->\r
+    icon = new Pettanr.View.Minicon(Pettanr.View.Image.icon_destroy_file())\r
+    Pettanr.View.mini_any_button(this,  @destroy_url(), icon, options)\r
+  \r
+  label_button: (label_options, button_options) ->\r
+    url = button_options.url || @show_url()\r
+    new Pettanr.View.Button(url, _.escape(@label(label_options)), button_options)\r
+  \r
+  #  faced_label_button({\r
+  #      shorten: true\r
+  #    }, {\r
+  #      url: @show_url(),\r
+  #      context: this,\r
+  #      click: () =>\r
+  #        # ...\r
+  #    })\r
+  faced_label_button: (label_options, button_options) ->\r
+    new Pettanr.View.FacedLabelButton(this, label_options, button_options)\r
+  \r
+  mini_faced_label_button: (label_options, button_options) ->\r
+    new Pettanr.View.MiniFacedLabelButton(this, label_options, button_options)\r
+  \r
+  summary: (context, options) ->\r
+    klass = Pettanr.Views[@singular()].Summary\r
+    new klass(this, context, options)\r
   \r
   @index_url: () ->\r
     Pettanr.url(@table_name(), 'index', {id: null})\r
@@ -344,6 +426,9 @@ class Peta.Item extends Backbone.Model
   release: () ->\r
     Pettanr.cache.release(this)\r
   \r
+  free: () ->\r
+    Pettanr.cache.free(this)\r
+  \r
   save: (model_attr) ->\r
     super(model_attr || @attributes, {\r
       success: (model, response, options) =>\r
@@ -355,6 +440,7 @@ class Peta.Item extends Backbone.Model
   destroy: () ->\r
     super({\r
       success: (model, response, options) =>\r
+        @free()\r
         @trigger('destroy:success', model, response)\r
       error: (model, response, options) =>\r
         @trigger('destroy:fail', model, response)\r