better handling of destroy function #42

Closed
wants to merge 1 commit into
from
View
BIN .DS_Store
Binary file not shown.
View
2 lib/generators/backbone/scaffold/templates/views/index_view.coffee
@@ -12,7 +12,7 @@ class <%= view_namespace %>.IndexView extends Backbone.View
@options.<%= plural_model_name %>.each(@addOne)
addOne: (<%= singular_model_name %>) ->
- view = new <%= view_namespace %>.<%= singular_name.camelize %>View({model : <%= singular_model_name %>})
+ view = new <%= view_namespace %>.<%= singular_name.camelize %>View({model : <%= singular_model_name %>, collection: @options.<%= plural_model_name %> })
@$("tbody").append(view.render().el)
render: ->
View
17 lib/generators/backbone/scaffold/templates/views/model_view.coffee
@@ -9,11 +9,20 @@ class <%= view_namespace %>.<%= singular_name.camelize %>View extends Backbone.V
tagName: "tr"
destroy: () ->
- @model.destroy()
- this.remove()
-
+ getViewAndCollection = ((view) -> return -> {collection: view.collection, view: view})(this)
+
+ @model.destroy
+ success: (model, response) ->
+ vars = getViewAndCollection()
+ vars.collection.remove model
+ vars.view.remove()
+ error: (model, response) ->
+ # handle error as you desire
+ # remember to change the custom Rails destroy controller action
+ # change format.json { head :ok } with format.json { render json: @user }
+
return false
render: ->
$(this.el).html(@template(@model.toJSON() ))
- return this
+ return this
View
4 test/generators/scaffold_generator_test.rb
@@ -41,7 +41,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "#{backbone_path}/views/posts/index_view.js.coffee" do |view|
assert_match /#{Regexp.escape('JST["backbone/templates/posts/index"]')}/, view
assert_match /#{Regexp.escape('@template(posts: @options.posts.toJSON() ))')}/, view
- assert_match /#{Regexp.escape("new Dummy.Views.Posts.PostView({model : post})")}/, view
+ assert_match /#{Regexp.escape("new Dummy.Views.Posts.PostView({model : post, collection: @options.posts })")}/, view
end
assert_file "#{backbone_path}/views/posts/show_view.js.coffee" do |view|
@@ -79,7 +79,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "#{backbone_path}/views/blog_posts/index_view.js.coffee" do |view|
assert_match /#{Regexp.escape('JST["backbone/templates/blog_posts/index"]')}/, view
assert_match /#{Regexp.escape('@template(blogPosts: @options.blogPosts.toJSON() ))')}/, view
- assert_match /#{Regexp.escape("new Dummy.Views.BlogPosts.BlogPostView({model : blogPost})")}/, view
+ assert_match /#{Regexp.escape("new Dummy.Views.BlogPosts.BlogPostView({model : blogPost, collection: @options.blogPosts })")}/, view
end
assert_file "#{backbone_path}/views/blog_posts/show_view.js.coffee" do |view|