OSDN Git Service

fix: fetch err
[pettanr/pettanr.git] / app / assets / javascripts / peta / item.js.coffee
index f6fcde4..f5fa53b 100644 (file)
@@ -1,4 +1,12 @@
 class Peta.Item extends Backbone.Model\r
+  \r
+  initialize: (attr = {}, options = {}) ->\r
+    @url = @default_url()\r
+    super(attr, options)\r
+    @expire_time = options.expire_time\r
+    @boosters = {}\r
+    @templates = {}\r
+  \r
   @child_models: () ->\r
     @my_manifest().child_models()\r
   \r
@@ -7,11 +15,14 @@ class Peta.Item extends Backbone.Model
   \r
   # ClassMethods\r
   \r
+  @default_url: () ->\r
+    '/' + @table_name() + '/'\r
+  \r
   @my_class: () ->\r
     this\r
   \r
   my_class: () ->\r
-    Pettanr[@constructor.name]\r
+    @constructor\r
   \r
   @my_peta: () ->\r
     return null if not Manifest.manifest().items\r
@@ -25,7 +36,8 @@ class Peta.Item extends Backbone.Model
     'Name'\r
   \r
   @plural: () ->\r
-    'Names'\r
+    table_name = Manifest.manifest().pluralize @item_name()\r
+    Pettanr.camelize(table_name)\r
   \r
   @class_name: () ->\r
     @singular()\r
@@ -54,15 +66,12 @@ class Peta.Item extends Backbone.Model
   @parent_model: () ->\r
     null\r
   \r
+  @has_picture: () ->\r
+    false\r
+  \r
   @path_name: (with_engine = false) ->\r
     @table_name()\r
   \r
-  @pickup_item_name: () ->\r
-    @item_name()\r
-  \r
-  @pickup_column_name: () ->\r
-    'id'\r
-  \r
   @find_boost_name: (column_name) ->\r
     @my_peta().find_boost_name column_name\r
   \r
@@ -75,13 +84,27 @@ class Peta.Item extends Backbone.Model
       if my_settings\r
         attr[manifest.settings_column_name] = my_settings.to_json\r
   \r
+  @retrieve: (id, context, options = {}) ->\r
+    item = new this({id: id})\r
+    item.retrieve(context, options)\r
+  \r
+  @trace_routes: () ->\r
+    {}\r
+  \r
+  @default_label_shorten_length = 13\r
+  \r
   #InstanceMethods\r
   \r
+  default_url: () ->\r
+    r = @my_class().default_url()\r
+    r = r.concat( @id ) if @id\r
+    r\r
+  \r
   singular: () ->\r
-    Pettanr[@constructor.name].singular()\r
+    @my_class().singular()\r
   \r
   plural: () ->\r
-    Pettanr[@constructor.name].plural()\r
+    @my_class().plural()\r
   \r
   item_name: () ->\r
     @my_class().item_name()\r
@@ -95,15 +118,6 @@ class Peta.Item extends Backbone.Model
   path_name: (with_engine = false) ->\r
     @my_class().path_name(with_engine)\r
   \r
-  pickup_item_name: () ->\r
-    @my_class().pickup_item_name()\r
-  \r
-  pickup_column_name: () ->\r
-    @my_class().pickup_column_name()\r
-  \r
-  pickup_id: () ->\r
-    @get(pickup_column_name())\r
-  \r
   form_template: (with_engine = false) ->\r
     @path_name(with_engine) + '/form'\r
   \r
@@ -113,34 +127,324 @@ 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
+    routes = [routes] if _.isString(routes)\r
+    route = routes.shift()\r
+    if _.isEmpty(routes)\r
+      # fetching terminate association. callback\r
+      cxt = options.context || context\r
+      @fetch_association(route, cxt, {\r
+        success: (association_item, options) =>\r
+          options.success.call(context, association_item)\r
+        context: context,\r
+        options: options\r
+      })\r
+    else\r
+      # fetching through associations\r
+      @fetch_association(route, this, {\r
+        success: (association_item, options) =>\r
+          association_item.get_association(routes, this, options)\r
+        context: context,\r
+        options: options\r
+      })\r
+  \r
+  fetch_association: (name, context, 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
+      @get_parent(name, context, fetch_options)\r
+    else if a.has_many[name]\r
+      @get_children(name, context, fetch_options)\r
+    else if a.has_one[name]\r
+      @get_child(name, context, fetch_options)\r
+    else\r
+      console.error('association does not exist in model manifest')\r
+  \r
+  get_parent: (belongs_to_name, context, options = null) ->\r
+    m = Manifest.item_name_to_model(belongs_to_name)\r
+    retriever = new Pettanr.Cache.Retriever(m, @get(belongs_to_name + '_id'))\r
+    return retriever if !options\r
+    @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
+    list = @has_one(has_one_name)\r
+    list.open(context, {\r
+      success: (items) =>\r
+        callback = options.success\r
+        item = items[0]\r
+        callback.call(context, item)\r
+    })\r
+  \r
+  get_children: (has_many_name, context, options = null) ->\r
+    list = @has_many(has_many_name)\r
+    list.open(context, {\r
+      success: (items) =>\r
+        callback = options.success\r
+        callback.call(context, items)\r
+    })\r
+  \r
+  has_many: (has_many_name) ->\r
+    has_many_manifest = @my_class().my_manifest().associations.has_many[has_many_name]\r
+    action_name = has_many_manifest.list_action_name\r
+    Locmare.ListGroup.list(\r
+      has_many_name, action_name, {id: @get('id')}\r
+    )\r
+  \r
+  has_one: (has_one_name) ->\r
+    has_one_manifest = @my_class().my_manifest().associations.has_one[has_one_name]\r
+    controller_name = has_one_manifest.model().table_name()\r
+    action_name = has_one_manifest.list_action_name\r
+    Locmare.ListGroup.list(\r
+      controller_name, action_name, {id: @get('id')}\r
+    )\r
+  \r
+  trace_to: (trace_name, context, options) ->\r
+    routes = @my_class().trace_routes()[trace_name]\r
+    if !routes\r
+      console.error('no trace route')\r
+      return\r
+    @get_association(routes, this, {\r
+      success: (association) => \r
+        options.success.call(context, association)\r
+    })\r
+  \r
   boosts: (level) ->\r
-    _this = this\r
     c = @my_class().my_peta().boost\r
-    _.each c, (boost_manifest, boost_name) ->\r
+    _.each c, (boost_manifest, boost_name) =>\r
       return if level == 'read' and boost_manifest.level == 'post'\r
-      _this.boost boost_manifest\r
+      @boost boost_manifest\r
   \r
   boost: (boost_manifest) ->\r
-    @boosters ||= {}\r
     @boosters[boost_manifest.name] = new Locmare.Booster(boost_manifest, this)\r
     # fetched item is not cleared .  force write booster\r
     #@boosters[boost_manifest.name] ||= new Locmare.Booster(boost_manifest, this)\r
   \r
-  boosters: () ->\r
-    @boosters ||= {}\r
-  \r
   is_extend_column: (column_name) ->\r
     @is_extend_column column_name\r
   \r
-  is_visible: (operators) ->\r
+  is_visible: (operators = Pettanr.cache.operators) ->\r
     if Manifest.manifest().magic_numbers['run_mode'] == 0\r
       return false if not operators.is_guest()\r
     else\r
       return false if not operators.is_resource_reader()\r
     true\r
   \r
-  icon_view: (half) ->\r
-    new Pettanr.Views[@my_class().singular()].Icon({item: this, half: half})\r
+  retriever: () ->\r
+    new Pettanr.Cache.Retriever(@my_class(), @get('id'))\r
+  \r
+  retrieve: (context, options = {}) ->\r
+    retriever = new Pettanr.Cache.Retriever(@my_class(), @get('id'))\r
+    @listenTo(retriever, 'retrieve', (item) =>\r
+      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
+    null\r
+  \r
+  @pick_model: () ->\r
+    Manifest.item_name_to_model(@pick_item_name())\r
+  \r
+  @traceable_item_names: () ->\r
+    []\r
+  \r
+  @is_traceable: (item_name) ->\r
+    _.contains(@traceable_item_names(), item_name)\r
+  \r
+  @is_picker_inspire: (item_name) ->\r
+    item_name == @item_name()\r
+  \r
+  @is_picker_trace: (item_name) ->\r
+    @is_traceable(item_name)\r
+  \r
+  @is_picker_pick: (item_name) ->\r
+    item_name == @pick_item_name()\r
+  \r
+  @pick_type: (item_name) ->\r
+    if @is_picker_inspire(item_name)\r
+      'inspire'\r
+    else if @is_picker_trace(item_name)\r
+      'trace'\r
+    else if @is_picker_pick(item_name)\r
+      'pick'\r
+    else\r
+      'none'\r
+  \r
+  symbol_option: (context, options) ->\r
+    @trace_to('symbol', this, {\r
+      success: (symbol_item) => \r
+        options.success.call(context, symbol_item.symbol_file())\r
+    })\r
+  \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
+  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
+  \r
+  index_url: () ->\r
+    @my_class().index_url()\r
+  \r
+  list_url: (action_name = 'index') ->\r
+    Pettanr.url(@table_name(), action_name, {id: @get('id')})\r
+  \r
+  show_url: () ->\r
+    Pettanr.url(@table_name(), 'show', {id: @get('id')})\r
+  \r
+  prof_url: () ->\r
+    Pettanr.url(@table_name(), 'show', {id: @get('id'), format: 'prof'})\r
+  \r
+  new_url: () ->\r
+    Pettanr.url(@table_name(), 'new', {})\r
+  \r
+  create_url: () ->\r
+    Pettanr.url(@table_name(), 'create', {})\r
+  \r
+  edit_url: () ->\r
+    Pettanr.url(@table_name(), 'edit', {id: @get('id')})\r
+  \r
+  update_url: () ->\r
+    Pettanr.url(@table_name(), 'update', {id: @get('id')})\r
+  \r
+  destroy_url: () ->\r
+    Pettanr.url(@table_name(), 'destroy', {id: @get('id')})\r
+  \r
+  hold: () ->\r
+    Pettanr.cache.hold(this)\r
+  \r
+  fix: () ->\r
+    Pettanr.cache.fix(this)\r
+  \r
+  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
+        @trigger('save:success', model, response)\r
+      error: (model, response, options) =>\r
+        @trigger('save:fail', model, response)\r
+    })\r
+  \r
+  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
+    })\r
   \r
   is_editize: () ->\r
     @editor\r
@@ -148,6 +452,9 @@ class Peta.Item extends Backbone.Model
   dom_id: () ->\r
     (@get('id') || '').toString()\r
   \r
+  cache_key: () ->\r
+    @table_name() + '-' + @dom_id()\r
+  \r
   dom_pool_type: () ->\r
     @new_record ? 'stored' : 'new'\r
   \r