define(['underscore', 'backbone', 'jquery'], function (_, Backbone, $) {
return (root.Backbone = factory(_, Backbone, $));
});
- } else if (typeof exports !== 'undefined') {
+ } else if (typeof exports !== 'undefined' && typeof require !== 'undefined') {
module.exports = factory(require('underscore'), require('backbone'), require('jquery'));
} else {
// Browser globals
}
// Shared methods
- function getCacheKey(instance, opts) {
- var url;
-
- if(opts && opts.url) {
- url = opts.url;
- } else {
- url = _.isFunction(instance.url) ? instance.url() : instance.url;
+ function getCacheKey(key, opts) {
+ if (key && _.isObject(key)) {
+ // If the model has its own, custom, cache key function, use it.
+ if (_.isFunction(key.getCacheKey)) {
+ return key.getCacheKey(opts);
+ }
+ // else, use the URL
+ if (opts && opts.url) {
+ key = opts.url;
+ } else {
+ key = _.isFunction(key.url) ? key.url() : key.url;
+ }
+ } else if (_.isFunction(key)) {
+ return key(opts);
}
-
- // Need url to use as cache key so return if we can't get it
- if(!url) { return; }
-
- if(opts && opts.data) {
+ if (opts && opts.data) {
if(typeof opts.data === 'string') {
- return url + '?' + opts.data;
+ return key + '?' + opts.data;
} else {
- return url + '?' + $.param(opts.data);
+ return key + '?' + $.param(opts.data);
}
}
- return url;
+ return key;
}
function setCache(instance, opts, attrs) {
opts = (opts || {});
var key = Backbone.fetchCache.getCacheKey(instance, opts),
- expires = false;
+ expires = false,
+ lastSync = (opts.lastSync || (new Date()).getTime()),
+ prefillExpires = false;
// Need url to use as cache key so return if we can't get it
if (!key) { return; }
expires = (new Date()).getTime() + ((opts.expires || 5 * 60) * 1000);
}
+ if (opts.prefillExpires !== false) {
+ prefillExpires = (new Date()).getTime() + ((opts.prefillExpires || 5 * 60) * 1000);
+ }
+
Backbone.fetchCache._cache[key] = {
expires: expires,
+ lastSync : lastSync,
+ prefillExpires: prefillExpires,
value: attrs
};
Backbone.fetchCache.setLocalStorage();
}
- function clearItem(key) {
- if (_.isFunction(key)) { key = key(); }
+ function getCache(key, opts) {
+ if (_.isFunction(key)) {
+ key = key();
+ } else if (key && _.isObject(key)) {
+ key = getCacheKey(key, opts);
+ }
+
+ return Backbone.fetchCache._cache[key];
+ }
+
+ function getLastSync(key, opts) {
+ return getCache(key).lastSync;
+ }
+
+ function clearItem(key, opts) {
+ if (_.isFunction(key)) {
+ key = key();
+ } else if (key && _.isObject(key)) {
+ key = getCacheKey(key, opts);
+ }
delete Backbone.fetchCache._cache[key];
Backbone.fetchCache.setLocalStorage();
}
+
+ function reset() {
+ // Clearing all cache items
+ Backbone.fetchCache._cache = {};
+ }
function setLocalStorage() {
if (!supportLocalStorage || !Backbone.fetchCache.localStorage) { return; }
localStorage.setItem(Backbone.fetchCache.getLocalStorageKey(), JSON.stringify(Backbone.fetchCache._cache));
} catch (err) {
var code = err.code || err.number || err.message;
- if (code === 22) {
+ if (code === 22 || code === 1014) {
this._deleteCacheWithPriority();
} else {
throw(err);
}
opts = _.defaults(opts || {}, { parse: true });
var key = Backbone.fetchCache.getCacheKey(this, opts),
- data = Backbone.fetchCache._cache[key],
+ data = getCache(key),
expired = false,
+ prefillExpired = false,
attributes = false,
deferred = new $.Deferred(),
self = this;
+ function isPrefilling() {
+ return opts.prefill && (!opts.prefillExpires || prefillExpired);
+ }
+
function setData() {
if (opts.parse) {
attributes = self.parse(attributes, opts);
self.trigger('sync', self, attributes, opts);
// Notify progress if we're still waiting for an AJAX call to happen...
- if (opts.prefill) { deferred.notify(self); }
+ if (isPrefilling()) { deferred.notify(self); }
// ...finish and return if we're not
else {
if (_.isFunction(opts.success)) { opts.success(self, attributes, opts); }
if (data) {
expired = data.expires;
expired = expired && data.expires < (new Date()).getTime();
+ prefillExpired = data.prefillExpires;
+ prefillExpired = prefillExpired && data.prefillExpires < (new Date()).getTime();
attributes = data.value;
}
setData();
}
- if (!opts.prefill) {
+ if (!isPrefilling()) {
return deferred;
}
}
opts = _.defaults(opts || {}, { parse: true });
var key = Backbone.fetchCache.getCacheKey(this, opts),
- data = Backbone.fetchCache._cache[key],
+ data = getCache(key),
expired = false,
+ prefillExpired = false,
attributes = false,
deferred = new $.Deferred(),
self = this;
+ function isPrefilling() {
+ return opts.prefill && (!opts.prefillExpires || prefillExpired);
+ }
+
function setData() {
self[opts.reset ? 'reset' : 'set'](attributes, opts);
if (_.isFunction(opts.prefillSuccess)) { opts.prefillSuccess(self); }
self.trigger('sync', self, attributes, opts);
// Notify progress if we're still waiting for an AJAX call to happen...
- if (opts.prefill) { deferred.notify(self); }
+ if (isPrefilling()) { deferred.notify(self); }
// ...finish and return if we're not
else {
if (_.isFunction(opts.success)) { opts.success(self, attributes, opts); }
if (data) {
expired = data.expires;
expired = expired && data.expires < (new Date()).getTime();
+ prefillExpired = data.prefillExpires;
+ prefillExpired = prefillExpired && data.prefillExpires < (new Date()).getTime();
attributes = data.value;
}
setData();
}
- if (!opts.prefill) {
+ if (!isPrefilling()) {
return deferred;
}
}
Backbone.fetchCache._superMethods = superMethods;
Backbone.fetchCache.setCache = setCache;
+ Backbone.fetchCache.getCache = getCache;
Backbone.fetchCache.getCacheKey = getCacheKey;
+ Backbone.fetchCache.getLastSync = getLastSync;
Backbone.fetchCache.clearItem = clearItem;
+ Backbone.fetchCache.reset = reset;
Backbone.fetchCache.setLocalStorage = setLocalStorage;
Backbone.fetchCache.getLocalStorage = getLocalStorage;
class Locmare.FormModule.FieldModule.TagModule.FileTag extends Locmare.FormModule.FieldModule.TagModule.Base\r
tagName: 'input'\r
+ events: {\r
+ 'change': 'change'\r
+ }\r
\r
render: () ->\r
this.$el.html('')\r
this.$el.attr(@attr())\r
this\r
\r
+ change: (event) ->\r
+ \r
type: () ->\r
'file'\r
\r
super(attr, options)\r
if @id\r
@url = @url + @id\r
- @bind('change:width', () ->\r
- console.log('cng')\r
- )\r
\r
super(attr, options)\r
if @id\r
@url = @url + @id\r
- @bind('change:width', () ->\r
- console.log('cng')\r
- )\r
\r
class Pettanr.PanelPicture.TraceFromPicture\r
\r
})\r
\r
save: () ->\r
- @listenTo(@item, 'save:success', @success)\r
- @listenTo(@item, 'save:fail', @fail)\r
- form_data = new FormData(this.$el.get(0))\r
attrs = {}\r
- _.each @fields, (field) =>\r
- if field.field_manifest.tag.type == 'file'\r
- form_data.append('original_picture[file]', field.tag.$el[0].files[0])\r
- else\r
- form_data.append(field.field_name, field.val())\r
+ form_data = new FormData(this.$el.get(0))\r
options = {\r
data: form_data,\r
processData: false,\r
- contentType: false\r
+ contentType: false,\r
+ success: (model, response, options) =>\r
+ @item = new Pettanr.OriginalPicture(model)\r
+ @trigger('success', model, response)\r
+ error: (model, response, options) =>\r
+ @item = new Pettanr.OriginalPicture(model)\r
+ @trigger('fail', model, response)\r
}\r
- @item.save(null, {}, options)\r
+ Backbone.sync('create', @item, options)\r
\r
}
end
- def render_destroy redirect_url = nil
+ def render_destroy redirect_url
if @item.destroy
respond_to do |format|
destroyed_html_format format, redirect_url
end
end
+ def render_destroy_by destroy_method_name, redirect_url = nil
+ if @item.__send__(destroy_method_name)
+ respond_to do |format|
+ destroyed_html_format format, redirect_url
+ destroyed_json_format format
+ end
+ else
+ respond_to do |format|
+ not_destroyed_html_format format
+ not_destroyed_json_format format
+ end
+ end
+ end
+
def format_filer format
format.html {
@paginate = @@model.list_paginate(@page, @page_size)
end
def destroy
- @artist = Artist.edit(params[:id], @operators)
- @artist.destroy
-
- respond_to do |format|
- format.html { redirect_to artists_url }
- format.json { head :ok }
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy '/home/configure'
end
+
end
@item.overwrite @operators
render_update '/home/configure'
end
+
+ def destroy
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy '/home/configure'
+ end
+
end
end
def destroy
- @comic_story = ComicStory.edit(params[:id], @operators)
- @comic = Comic.edit(@comic_story.comic_id, @operators) if @comic_story.comic_id
- respond_to do |format|
- if @comic_story.destroy_and_shorten
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => ComicStory.model_name.human)
- format.html { redirect_to play_comic_path(@comic) }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => ComicStory.model_name.human)
- format.html { redirect_to comic_story_path(@comic_story) }
- format.json { render json: @comic_story.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+ render_destroy '/home/' + play_comic_path(@item)
end
+
end
end
def destroy
- @item = Comic.edit(params[:id], @operators)
- respond_to do |format|
- if @item.destroy_with_leafs
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => Comic.model_name.human)
- format.html { redirect_to '/home/comics' }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Comic.model_name.human)
- format.html { redirect_to @item }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy '/home/' + @item.path_name
end
+
end
list_count
end
+ def destroy
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy @my_model_class.path_name
+ end
+
end
render_update
end
+ def destroy
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy @my_model_class.path_name
+ end
+
end
end
def destroy
- @item = self.class.model.edit(params[:id], @operators)
- respond_to do |format|
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => self.class.model.model_name.human)
- format.html { redirect_to @item }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy @my_model_class.path_name
end
+
end
def create
set_model
- p params[:file]
@imager = if params[:original_picture]
PettanImager.load set_image params[:original_picture][:file]
else
end
def destroy
- @item = OriginalPicture.edit(params[:id], @operators)
-
- respond_to do |format|
- if @item.destroy_with_resource_picture
- format.html { redirect_to original_pictures_url }
- format.json { head :ok }
- else
- format.html { redirect_to original_picture_path(@item) }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy_by 'destroy_with_resource_picture', original_pictures_url
end
+
end
end
def destroy
- @item = Panel.edit(params[:id], @operators)
- respond_to do |format|
- Panel.transaction do
- if @item.destroy_with_elements
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => Panel.model_name.human)
- format.html { redirect_to '/home/panels' }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Panel.model_name.human)
- format.html { redirect_to @item }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy '/home/' + @item.path_name
end
end
end
def destroy
- @resource_picture = ResourcePicture.edit(params[:id], @operators)
-
- respond_to do |format|
- if @resource_picture.unpublish
- format.html { redirect_to :controller => '/home', :action => :resource_pictures }
- format.json {
- render json: '{}', status: :ok
- }
- else
- format.html { redirect_to resource_picture_path(@resource_picture) }
- format.json { render json: @resource_picture.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy_by 'unpublish', '/home/' + @item.path_name
end
def count
end
def destroy
- @scroll_panel = ScrollPanel.edit(params[:id], @operators)
- @scroll = Scroll.edit(@scroll_panel.scroll_id, @operators) if @scroll_panel.scroll_id
- respond_to do |format|
- if @scroll_panel.destroy_and_shorten
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => ScrollPanel.model_name.human)
- format.html { redirect_to play_scroll_path(@scroll) }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => ScrollPanel.model_name.human)
- format.html { redirect_to scroll_panel_path(@scroll_panel) }
- format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+ render_destroy '/home/' + play_scroll_path(@item)
end
+
end
@item = @my_model_class.edit(params[:id], @operators)
render_destroy '/home/' + @item.path_name
end
+
end
end
def destroy
- @sheet = Sheet.edit(params[:id], @operators)
- respond_to do |format|
- if @sheet.destroy_with_sheet_panel
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => Sheet.model_name.human)
- format.html { redirect_to '/home/sheets' }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Sheet.model_name.human)
- format.html { redirect_to @sheet }
- format.json { render json: @sheet.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy '/home/' + @item.path_name
end
+
end
end
def destroy
- @item = SpeechBalloonTemplate.find(params[:id])
- @item.destroy
-
- respond_to do |format|
- format.html { redirect_to :action => :list }
- format.json { head :ok }
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy @my_model_class.path_name
end
+
end
end
def destroy
- @item = Story.edit(params[:id], @operators)
- respond_to do |format|
- if @item.destroy_with_leafs
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => Story.model_name.human)
- format.html { redirect_to '/home/stories' }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Story.model_name.human)
- format.html { redirect_to @item }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy '/home/' + @item.path_name
end
end
end
def destroy
- @story_sheet = StorySheet.edit(params[:id], @operators)
- @story = Story.edit(@story_sheet.story_id, @operators) if @story_sheet.story_id
- respond_to do |format|
- if @story_sheet.destroy_and_shorten
- flash[:notice] = I18n.t('flash.notice.destroyed', :model => StorySheet.model_name.human)
- format.html { redirect_to play_story_path(@story) }
- format.json { head :ok }
- else
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => StorySheet.model_name.human)
- format.html { redirect_to story_sheet_path(@story_sheet) }
- format.json { render json: @story_sheet.errors, status: :unprocessable_entity }
- end
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+ render_destroy '/home/' + play_story_path(@item)
end
+
end
end
def destroy
- @item = self.class.model.edit(params[:id], @operators)
- respond_to do |format|
- flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => self.class.model.model_name.human)
- format.html { redirect_to @item }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
+ set_model
+ @item = @my_model_class.edit(params[:id], @operators)
+ render_destroy @my_model_class.path_name
end
end
belongs_to :system_picture
belongs_to :artist
has_one :resource_picture
- has_one :resource_picture_pictures
+ has_one :resource_picture_picture
validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :revision, :presence => true, :numericality => true
end
def unpublish
+ self.boosts 'post'
imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
return false unless imager
self.store imager
def change
create_table :resource_picture_pictures do |t|
t.integer :original_picture_id, :null => false, :default => 0
- t.integer :resource_picture_id, :default => nil
+ t.integer :resource_picture_id, :null => false, :default => 0
t.integer :picture_id, :null => false, :default => 0
t.timestamps
--- /dev/null
+class RppIdAllowNull < ActiveRecord::Migration
+ def up
+ change_column_null :resource_picture_pictures, :original_picture_id, true
+ change_column_null :resource_picture_pictures, :resource_picture_id, true
+ change_column_default :resource_picture_pictures, :original_picture_id, nil
+ change_column_default :resource_picture_pictures, :resource_picture_id, nil
+ end
+
+ def down
+ end
+end
res = false
self.class.transaction do
self.leafs_items.each do |leaf|
- raise ActiveRecord::Rollback unless leaf.destroy_and_shorten
+ raise ActiveRecord::Rollback unless leaf.destroy
end
res = super
raise ActiveRecord::Rollback unless res
res
end
- def destroy_and_shorten
+ def destroy
res = false
self.class.transaction do
self.class.update_all('t = t - 1',
[self.binder_key + ' = ? and (t > ?)', self.binder_id, self.t]
)
- raise ActiveRecord::Rollback unless self.destroy
- res = true
+ res = super
+ raise ActiveRecord::Rollback unless res
end
res
end
res
end
- def destroy_with_elements
+ def destroy
res = false
self.class.transaction do
self.parts_element.each do |element|
raise ActiveRecord::Rollback unless element.destroy
end
- raise ActiveRecord::Rollback unless self.destroy
- res = true
+ res = super
+ raise ActiveRecord::Rollback unless res
end
res
end