Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updating views to use backbones auto attached options directly on the…

… view
  • Loading branch information...
commit a9d081b071f34c3a94998834db6b69a398c255b8 1 parent 24ca585
@ryanfitz ryanfitz authored
View
22 lib/generators/backbone/scaffold/templates/views/new_view.coffee
@@ -8,9 +8,9 @@ class <%= view_namespace %>.NewView extends Backbone.View
constructor: (options) ->
super(options)
- @options.model = new @options.collection.model()
+ @model = new @collection.model()
- @options.model.bind("change:errors", () =>
+ @model.bind("change:errors", () =>
this.render()
)
@@ -18,20 +18,20 @@ class <%= view_namespace %>.NewView extends Backbone.View
e.preventDefault()
e.stopPropagation()
- @options.model.unset("errors")
+ @model.unset("errors")
- @options.collection.create(@options.model.toJSON(),
- success: (model) =>
- @options.model = model
- window.location.hash = "/#{@options.model.id}"
+ @collection.create(@model.toJSON(),
+ success: (<%= singular_name %>) =>
+ @model = <%= singular_name %>
+ window.location.hash = "/#{@model.id}"
- error: (model, jqXHR) =>
- @options.model.set({errors: $.parseJSON(jqXHR.responseText)})
+ error: (<%= singular_name %>, jqXHR) =>
+ @model.set({errors: $.parseJSON(jqXHR.responseText)})
)
render: ->
- $(this.el).html(@template(@options.model.toJSON() ))
+ $(this.el).html(@template(@model.toJSON() ))
- this.$("form").backboneLink(@options.model)
+ this.$("form").backboneLink(@model)
return this
View
22 test/dummy/app/assets/javascripts/backbone/views/posts/new_view.js.coffee
@@ -8,9 +8,9 @@ class Dummy.Views.Posts.NewView extends Backbone.View
constructor: (options) ->
super(options)
- @options.model = new @options.collection.model()
+ @model = new @collection.model()
- @options.model.bind("change:errors", () =>
+ @model.bind("change:errors", () =>
this.render()
)
@@ -18,20 +18,20 @@ class Dummy.Views.Posts.NewView extends Backbone.View
e.preventDefault()
e.stopPropagation()
- @options.model.unset("errors")
+ @model.unset("errors")
- @options.collection.create(@options.model.toJSON(),
- success: (model) =>
- @options.model = model
- window.location.hash = "/#{@options.model.id}"
+ @collection.create(@model.toJSON(),
+ success: (post) =>
+ @model = post
+ window.location.hash = "/#{@model.id}"
- error: (model, jqXHR) =>
- @options.model.set({errors: $.parseJSON(jqXHR.responseText)})
+ error: (post, jqXHR) =>
+ @model.set({errors: $.parseJSON(jqXHR.responseText)})
)
render: ->
- $(this.el).html(@template(@options.model.toJSON() ))
+ $(this.el).html(@template(@model.toJSON() ))
- this.$("form").backboneLink(@options.model)
+ this.$("form").backboneLink(@model)
return this
View
8 test/generators/scaffold_generator_test.rb
@@ -52,9 +52,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "#{backbone_path}/views/posts/new_view.js.coffee" do |view|
assert_match /class Dummy.Views.Posts.NewView extends Backbone.View/, view
- assert_match /#{Regexp.escape('@template(@options.model.toJSON() )')}/, view
+ assert_match /#{Regexp.escape('@template(@model.toJSON() )')}/, view
assert_match /#{Regexp.escape('JST["backbone/templates/posts/new"]')}/, view
assert_match /#{Regexp.escape('"submit #new-post": "save"')}/, view
+ assert_match /#{Regexp.escape('success: (post) =>')}/, view
+ assert_match /#{Regexp.escape('@model = post')}/, view
end
assert_file "#{backbone_path}/views/posts/edit_view.js.coffee" do |view|
@@ -88,9 +90,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "#{backbone_path}/views/blog_posts/new_view.js.coffee" do |view|
assert_match /class Dummy.Views.BlogPosts.NewView extends Backbone.View/, view
- assert_match /#{Regexp.escape('@template(@options.model.toJSON() )')}/, view
+ assert_match /#{Regexp.escape('@template(@model.toJSON() )')}/, view
assert_match /#{Regexp.escape('JST["backbone/templates/blog_posts/new"]')}/, view
assert_match /#{Regexp.escape('"submit #new-blog_post": "save"')}/, view
+ assert_match /#{Regexp.escape('success: (blog_post) =>')}/, view
+ assert_match /#{Regexp.escape('@model = blog_post')}/, view
end
assert_file "#{backbone_path}/views/blog_posts/edit_view.js.coffee" do |view|
Please sign in to comment.
Something went wrong with that request. Please try again.