fix the annoying bug of duplicated flash messages + first backbone view for the index pages section

This commit is contained in:
did 2011-11-10 13:29:59 +01:00
parent aa1abd2e92
commit 6122983835
6 changed files with 77 additions and 14 deletions

View File

@ -5,6 +5,10 @@ class Locomotive.Views.ApplicationView extends Backbone.View
render: ->
@render_flash_messages(@options.flash)
if @options.view?
@view = new @options.view
@view.render()
return @
render_flash_messages: (messages) ->

View File

@ -0,0 +1,11 @@
Locomotive.Views.Pages ||= {}
class Locomotive.Views.Pages.IndexView extends Backbone.View
el: '#content'
render: ->
@index_view = new Locomotive.Views.Pages.ListView()
@index_view.render()
return @

View File

@ -0,0 +1,29 @@
Locomotive.Views.Pages ||= {}
class Locomotive.Views.Pages.ListView extends Backbone.View
el: '#pages-list'
render: ->
console.log('render list view');
@make_sortable()
return @
make_sortable: ->
# TODO
# $('#pages-list ul.folder').sortable({
# 'handle': 'em',
# 'axis': 'y',
# 'update': function(event, ui) {
# var params = $(this).sortable('serialize', { 'key': 'children[]' });
# params += '&_method=put';
# params += '&' + $('meta[name=csrf-param]').attr('content') + '=' + $('meta[name=csrf-token]').attr('content');
#
# $.post($(this).attr('data-url'), params, function(data) {
# var error = typeof(data.error) != 'undefined';
# $.growl((error ? 'error' : 'success'), (error ? data.error : data.notice));
# }, 'json');
# }
# });

View File

@ -1,8 +1,5 @@
- title t('.title')
/ - content_for :head do
/ = include_javascripts :pages
- content_for :submenu do
= render 'locomotive/shared/menu/contents'

View File

@ -18,7 +18,8 @@
$(document).ready(function() {
window.application_view = new Locomotive.Views.ApplicationView({
flash: #{flash.to_json}
flash: #{flash.to_json},
view: Locomotive.Views.#{controller.controller_name.camelize}.#{controller.action_name.camelize}View
});
window.application_view.render();

View File

@ -3,24 +3,45 @@ module Locomotive
include ::Responders::FlashResponder
def api_behavior(error)
raise error unless resourceful?
# generate flash messages
set_flash_message!
# by default flash_now messages if the resource has errors
def set_flash_now?
super || has_errors?
end
def to_json
if get?
display resource
elsif has_errors?
display({ :errors => resource.errors, :model => controller.send(:resource_instance_name), :alert => controller.flash[:alert] })
display resource.errors, :status => :unprocessable_entity
elsif post?
display resource, :status => :created, :location => resource_location
display resource, :status => :created, :location => api_location
elsif put?
display resource, :status => :ok, :location => api_location
elsif has_empty_resource_definition?
display empty_resource, :status => :ok
else
display({ :notice => controller.flash[:notice] })
head :ok
end
controller.flash.discard # reset flash messages !
end
# def api_behavior(error)
# raise error unless resourceful?
#
# # generate flash messages
# set_flash_message!
#
# if get?
# display resource
# elsif has_errors?
# display({ :errors => resource.errors, :model => controller.send(:resource_instance_name), :alert => controller.flash[:alert] })
# elsif post?
# display resource, :status => :created, :location => resource_location
# else
# display({ :notice => controller.flash[:notice] })
# end
#
# controller.flash.discard # reset flash messages !
# end
end
end