X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Fmodels%2Fpanel.js.coffee;h=c3479411e5d36e69a2be32a6ececb9f180f04aae;hb=6b12a345e25cadadaf2a4cc15b8f4967193d6afb;hp=2d900854155a74451601cffc59b934350d13b3c4;hpb=b922609711ac4713a21d2e64cac393765c0917e9;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/models/panel.js.coffee b/app/assets/javascripts/models/panel.js.coffee index 2d900854..c3479411 100644 --- a/app/assets/javascripts/models/panel.js.coffee +++ b/app/assets/javascripts/models/panel.js.coffee @@ -1,17 +1,5 @@ class Pettanr.Panel extends Peta.Root url: '/panels/' - relations: [ - { - type: Backbone.HasMany, - key: 'panel_pictures', - relatedModel: 'Pettanr.PanelPicture', - collectionType: 'Pettanr.PanelPictures', - reverseRelation: { - key: 'panel', - includeInJSON: 'id' - } - }, - ] @singular: () -> 'Panel' @@ -25,9 +13,15 @@ class Pettanr.Panel extends Peta.Root publish: 0 } + author: () -> + new Pettanr.Author({id: @get('author_id')}) + pp: () -> new Pettanr.PanelPicture({id: @get('pp_id')}) + with_elements: () -> + new Pettanr.Panel.WithElements({id: @get('id')}) + is_visible: (operators) -> switch super(operators) when null # super return @@ -38,33 +32,57 @@ class Pettanr.Panel extends Peta.Root return true if @isNew() @is_publish() + overwrite: (options) -> + @set('author_id', options.operators.author.id) + is_usable: (operators) -> @is_visible(operators) is_publish: () -> @get('publish') > 0 + @licensed_pictures: (panels) -> + r = {} + _.each panels, (panel) -> + _.extend r, panel.licensed_pictures() + r + + licensed_pictures: () -> + r = {} + _.each @scenario_elements(), (elm) -> + return if not elm.my_class().has_picture() + pid = elm.get('picture_id') + r[pid] = elm.picture() if not r[pid] + r + + my_url: () -> + '/' + @path_name() + '/' + @get('id') + initialize: (attr = {}, options = {}) -> @set(attr) if @get('id') - @url = @url + @get('id') - if options.with_elements - @url = @url + '?with_elements=true' + @url = @my_url() + @bind('change:width', () -> + console.log('cng') + ) -class Pettanr.Panels extends Backbone.Collection - model: Pettanr.Panel - url: '/panels/' -class Pettanr.RelationPanel extends Pettanr.Panel - relations: [ - { - type: Backbone.HasMany, - key: 'panel_pictures', - relatedModel: 'Pettanr.PanelPicture', - collectionType: 'Pettanr.PanelPictures', - reverseRelation: { - key: 'panel', - includeInJSON: 'id' - } - }, - ] +class Pettanr.Panel.WithElements extends Pettanr.Panel + + fetch: (options) -> + @url = @my_url() + '?with_elements=true' + super(options) + + save: (attrs, options = {}) -> + @url = @my_url() + options.attrs = {panel: attrs} + method = if @isNew() + 'create' + else + 'update' + xhr = @sync(method, this, options) + xhr + + destroy: (options) -> + @url = @my_url() + super(options)