delete snippets in ajax + new way of render theme assets in the back-office (backbone view) + change the way to fire growl messages
This commit is contained in:
parent
dd15e3a758
commit
af955ef927
@ -10,4 +10,6 @@ window.Locomotive =
|
|||||||
Models: {}
|
Models: {}
|
||||||
Collections: {}
|
Collections: {}
|
||||||
Routers: {}
|
Routers: {}
|
||||||
Views: {}
|
Views: {}
|
||||||
|
|
||||||
|
window.Locomotive.Views.Memberships = {}
|
@ -4,4 +4,8 @@ class Locomotive.Models.Snippet extends Backbone.Model
|
|||||||
|
|
||||||
urlRoot: "#{Locomotive.mount_on}/snippets"
|
urlRoot: "#{Locomotive.mount_on}/snippets"
|
||||||
|
|
||||||
class Locomotive.Models.SnippetsCollection extends Backbone.Collection
|
class Locomotive.Models.SnippetsCollection extends Backbone.Collection
|
||||||
|
|
||||||
|
model: Locomotive.Models.Snippet
|
||||||
|
|
||||||
|
url: "#{Locomotive.mount_on}/snippets"
|
@ -14,8 +14,6 @@ class Locomotive.Views.MyAccount.EditView extends Locomotive.Views.Shared.FormVi
|
|||||||
|
|
||||||
Backbone.ModelBinding.bind @
|
Backbone.ModelBinding.bind @
|
||||||
|
|
||||||
window.foo = @model
|
|
||||||
|
|
||||||
render: ->
|
render: ->
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
@ -11,8 +11,6 @@ class Locomotive.Views.Pages.EditView extends Locomotive.Views.Pages.FormView
|
|||||||
success: (model, response, xhr) =>
|
success: (model, response, xhr) =>
|
||||||
model._normalize()
|
model._normalize()
|
||||||
|
|
||||||
$.growl('success', xhr.getResponseHeader('X-Message'))
|
|
||||||
|
|
||||||
if model.get('template_changed') == true
|
if model.get('template_changed') == true
|
||||||
@reset_editable_elements()
|
@reset_editable_elements()
|
||||||
else
|
else
|
||||||
@ -23,7 +21,5 @@ class Locomotive.Views.Pages.EditView extends Locomotive.Views.Pages.FormView
|
|||||||
|
|
||||||
@show_errors errors
|
@show_errors errors
|
||||||
|
|
||||||
$.growl('error', xhr.getResponseHeader('X-Message'))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,19 +29,11 @@ class Locomotive.Views.Shared.FormView extends Backbone.View
|
|||||||
|
|
||||||
@model.save {},
|
@model.save {},
|
||||||
success: (model, response, xhr) =>
|
success: (model, response, xhr) =>
|
||||||
window.response = xhr
|
|
||||||
|
|
||||||
$.growl('success', xhr.getResponseHeader('X-Message')) if xhr.getResponseHeader('X-Message')?
|
|
||||||
|
|
||||||
model.attributes = previous_attributes
|
model.attributes = previous_attributes
|
||||||
|
|
||||||
options.on_success(response, xhr) if options.on_success
|
options.on_success(response, xhr) if options.on_success
|
||||||
|
|
||||||
error: (model, xhr) =>
|
error: (model, xhr) =>
|
||||||
$.growl('error', xhr.getResponseHeader('X-Message'))
|
|
||||||
|
|
||||||
console.log(xhr)
|
|
||||||
|
|
||||||
errors = JSON.parse(xhr.responseText)
|
errors = JSON.parse(xhr.responseText)
|
||||||
|
|
||||||
@show_errors errors
|
@show_errors errors
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
Locomotive.Views.Snippets ||= {}
|
||||||
|
|
||||||
|
class Locomotive.Views.Snippets.ItemView extends Backbone.View
|
||||||
|
|
||||||
|
tagName: 'li'
|
||||||
|
|
||||||
|
events:
|
||||||
|
'click a.remove': 'remove_snippet'
|
||||||
|
|
||||||
|
render: ->
|
||||||
|
$(@el).html(ich.snippet_item(@model.toJSON()))
|
||||||
|
|
||||||
|
return @
|
||||||
|
|
||||||
|
remove_snippet: (event) ->
|
||||||
|
event.stopPropagation() & event.preventDefault()
|
||||||
|
|
||||||
|
if confirm $(event.target).attr('data-confirm')
|
||||||
|
@model.destroy()
|
@ -0,0 +1,47 @@
|
|||||||
|
Locomotive.Views.Snippets ||= {}
|
||||||
|
|
||||||
|
class Locomotive.Views.Snippets.ListView extends Backbone.View
|
||||||
|
|
||||||
|
tagName: 'div'
|
||||||
|
|
||||||
|
className: 'box'
|
||||||
|
|
||||||
|
_item_views = []
|
||||||
|
|
||||||
|
initialize: ->
|
||||||
|
_.bindAll(@, 'remove_item')
|
||||||
|
@collection = new Locomotive.Models.SnippetsCollection(@options.collection)
|
||||||
|
@collection.bind('remove', @remove_item)
|
||||||
|
|
||||||
|
render: ->
|
||||||
|
$(@el).html(ich.snippets_list())
|
||||||
|
|
||||||
|
@render_items()
|
||||||
|
|
||||||
|
return @
|
||||||
|
|
||||||
|
render_items: ->
|
||||||
|
if @collection.length == 0
|
||||||
|
@$('.no-items').show()
|
||||||
|
else
|
||||||
|
@collection.each (snippet) =>
|
||||||
|
@insert_item(snippet)
|
||||||
|
|
||||||
|
insert_item: (snippet) ->
|
||||||
|
view = new Locomotive.Views.Snippets.ItemView model: snippet, parent_view: @
|
||||||
|
|
||||||
|
(@_item_views ||= []).push(view)
|
||||||
|
|
||||||
|
@$('ul').append(view.render().el)
|
||||||
|
|
||||||
|
remove_item: (snippet) ->
|
||||||
|
@$('.no-items').show() if @collection.length == 0
|
||||||
|
view = _.find @_item_views, (tmp) -> tmp.model == snippet
|
||||||
|
view.remove() if view?
|
||||||
|
|
||||||
|
remove: ->
|
||||||
|
_.each @_item_views, (view) => view.remove()
|
||||||
|
super
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -4,30 +4,45 @@ class Locomotive.Views.ThemeAssets.IndexView extends Backbone.View
|
|||||||
|
|
||||||
el: '#content'
|
el: '#content'
|
||||||
|
|
||||||
events:
|
# events:
|
||||||
'click .box a.remove': 'remove_asset'
|
# 'click .box a.remove': 'remove_asset'
|
||||||
|
|
||||||
|
# initialize: ->
|
||||||
|
|
||||||
render: ->
|
render: ->
|
||||||
|
@render_snippets()
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
remove_asset: (event) ->
|
render_snippets: ->
|
||||||
event.stopPropagation() & event.preventDefault()
|
@snippets_view = new Locomotive.Views.Snippets.ListView collection: @options.snippets
|
||||||
|
|
||||||
link = $(event.target)
|
@$('#snippets-anchor').replaceWith(@snippets_view.render().el)
|
||||||
|
|
||||||
$.rails.ajax
|
remove: ->
|
||||||
url: link.attr('href')
|
@snippets_view.remove()
|
||||||
type: 'post'
|
super
|
||||||
dataType: 'json'
|
|
||||||
data:
|
|
||||||
_method: 'delete'
|
|
||||||
success: @on_successful_delete
|
|
||||||
error: @on_failed_delete
|
|
||||||
|
|
||||||
on_successful_delete: (data, status, xhr) ->
|
# remove_asset: (event) ->
|
||||||
console.log('youpi')
|
# event.stopPropagation() & event.preventDefault()
|
||||||
$.growl('success', xhr.getResponseHeader('X-Message'))
|
#
|
||||||
|
# link = $(event.target)
|
||||||
on_failed_delete: (data, status, xhr) ->
|
#
|
||||||
$.growl('error', xhr.getResponseHeader('X-Message'))
|
# if confirm(link.attr('data-confirm'))
|
||||||
|
# $.rails.ajax
|
||||||
|
# url: link.attr('href')
|
||||||
|
# type: 'post'
|
||||||
|
# dataType: 'json'
|
||||||
|
# data:
|
||||||
|
# _method: 'delete'
|
||||||
|
# success: (data, status, xhr) => @on_successful_delete(link, xhr)
|
||||||
|
# error: @on_failed_delete
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# on_successful_delete: (link, xhr) ->
|
||||||
|
# link.parents('')
|
||||||
|
# $.growl('success', xhr.getResponseHeader('X-Message'))
|
||||||
|
#
|
||||||
|
# on_failed_delete: (data, status, xhr) ->
|
||||||
|
# $.growl('error', xhr.getResponseHeader('X-Message'))
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ module Locomotive
|
|||||||
|
|
||||||
sections 'settings', 'theme_assets'
|
sections 'settings', 'theme_assets'
|
||||||
|
|
||||||
respond_to :json, :only => [:create, :update]
|
respond_to :json, :only => [:create, :update, :destroy]
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@snippet = current_site.snippets.new
|
@snippet = current_site.snippets.new
|
||||||
|
@ -3,7 +3,7 @@ module Locomotive
|
|||||||
|
|
||||||
sections 'settings', 'theme_assets'
|
sections 'settings', 'theme_assets'
|
||||||
|
|
||||||
respond_to :json, :only => [:index, :create, :update]
|
respond_to :json, :only => [:index, :create, :update, :destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
@ -3,8 +3,12 @@ module Locomotive
|
|||||||
|
|
||||||
delegate :name, :slug, :template, :to => :source
|
delegate :name, :slug, :template, :to => :source
|
||||||
|
|
||||||
|
def updated_at
|
||||||
|
I18n.l(self.source.updated_at, :format => :short)
|
||||||
|
end
|
||||||
|
|
||||||
def included_methods
|
def included_methods
|
||||||
super + %w(name slug template)
|
super + %w(name slug template updated_at)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
%li
|
%script{ :type => 'text/html', :id => 'snippet_item' }
|
||||||
%strong= link_to snippet.name, edit_snippet_path(snippet)
|
|
||||||
|
%strong= link_to "{{name}}", edit_snippet_path('{{id}}')
|
||||||
.more
|
.more
|
||||||
%span!= t('.updated_at')
|
%span!= t('.updated_at')
|
||||||
= l snippet.updated_at, :format => :short
|
{{updated_at}}
|
||||||
|
|
||||||
= link_to 'x', snippet_path(snippet), :class => 'remove', :confirm => t('locomotive.messages.confirm'), :method => :delete
|
= link_to 'x', '#', :class => 'remove', :confirm => t('locomotive.messages.confirm')
|
||||||
|
7
app/views/locomotive/theme_assets/_list.html.haml
Normal file
7
app/views/locomotive/theme_assets/_list.html.haml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
%script{ :type => 'text/html', :id => "#{name}_list" }
|
||||||
|
|
||||||
|
%h3!= title
|
||||||
|
.inner
|
||||||
|
%p.no-items{ :style => 'display: none' }!= empty_message
|
||||||
|
|
||||||
|
%ul{ :class => name.dasherize }
|
@ -1,5 +1,9 @@
|
|||||||
- title t('.title')
|
- title t('.title')
|
||||||
|
|
||||||
|
- content_for :backbone_view_data do
|
||||||
|
:plain
|
||||||
|
{ snippets: #{@snippets.to_json} }
|
||||||
|
|
||||||
- content_for :submenu do
|
- content_for :submenu do
|
||||||
= render_cell 'locomotive/settings_menu', :show
|
= render_cell 'locomotive/settings_menu', :show
|
||||||
|
|
||||||
@ -10,14 +14,20 @@
|
|||||||
%p!= t('.help')
|
%p!= t('.help')
|
||||||
|
|
||||||
- if can?(:manage, Locomotive::Snippet)
|
- if can?(:manage, Locomotive::Snippet)
|
||||||
.box
|
#snippets-anchor
|
||||||
%h3!= t('.snippets')
|
|
||||||
.inner
|
- content_for :head do
|
||||||
- if @snippets.empty?
|
= render '/locomotive/snippets/snippet'
|
||||||
%p.no-items!= t('locomotive.snippets.index.no_items', :url => new_snippet_url)
|
= render 'list', :name => 'snippets', :title => t('.snippets'), :empty_message => t('locomotive.snippets.index.no_items', :url => new_snippet_url)
|
||||||
- else
|
|
||||||
%ul.snippets
|
/ .box
|
||||||
= render @snippets
|
/ %h3!= t('.snippets')
|
||||||
|
/ .inner
|
||||||
|
/ - if @snippets.empty?
|
||||||
|
/ %p.no-items!= t('locomotive.snippets.index.no_items', :url => new_snippet_url)
|
||||||
|
/ - else
|
||||||
|
/ %ul.snippets
|
||||||
|
/ = render @snippets
|
||||||
|
|
||||||
- if can?(:manage, Locomotive::ThemeAsset)
|
- if can?(:manage, Locomotive::ThemeAsset)
|
||||||
.box
|
.box
|
||||||
|
@ -23,6 +23,10 @@ module Locomotive
|
|||||||
with_flash_message do |message|
|
with_flash_message do |message|
|
||||||
display resource, :status => :ok, :location => api_location
|
display resource, :status => :ok, :location => api_location
|
||||||
end
|
end
|
||||||
|
elsif delete?
|
||||||
|
with_flash_message do |message|
|
||||||
|
display resource, :status => :ok, :location => api_location
|
||||||
|
end
|
||||||
elsif has_empty_resource_definition?
|
elsif has_empty_resource_definition?
|
||||||
display empty_resource, :status => :ok
|
display empty_resource, :status => :ok
|
||||||
else
|
else
|
||||||
|
8
vendor/assets/javascripts/backbone.sync.js
vendored
8
vendor/assets/javascripts/backbone.sync.js
vendored
@ -88,6 +88,14 @@
|
|||||||
params.processData = false;
|
params.processData = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
params.complete = function(xhr, textStatus) {
|
||||||
|
var noticeType = xhr.getResponseHeader('X-Message-Type');
|
||||||
|
if (noticeType != null) {
|
||||||
|
var growlType = noticeType == 'notice' ? 'notice' : 'alert'; // for now, only 2 kind of growl messages
|
||||||
|
$.growl(noticeType, xhr.getResponseHeader('X-Message'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Make the request.
|
// Make the request.
|
||||||
return $.ajax(params);
|
return $.ajax(params);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user