Rails have Nested Resources for a while, and it has been heavy used (or overused). Say we have two model, Article and Comment.
class Article < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
end
Define the Nested Resource in routes.rb
resources :articles do
resources :comments
end
So now, we can list comments by specific article: http://localhost:3000/articles/1/comments
But Spine can only make url for post request to create Article and Comment like this:
/articles
/comments
How to make Spine's url for Ajax request like this?
/articles/1/comments
I know I can override the url() in Comment Model for retrieval comments, but what about creating a new record?
I also go through the source code as well, what I found is that the create() method in Spine's Ajax module doesn't care about the custom url() function in instance of Comment. what I want is just pass the article_id and using it with my custom url() function to generate url, then I can posting to server for create.
Does it possible without fork and modified version of Spine fo my own?
btw: sorry for my English, wish all of you guys can understand what I want to say about :-)
Thank you and best regards,
The model's url property can be a value or a function. So you could do:
class Comment extends Spine.Model
@configure "comment", "article_id"
@extend Spine.Model.Ajax
@url: ->
"articles/#{article_id}/comments"
or something similar. The ajax module will evaluate this property and use it as the resource end point when building requests.
add
#= require spine/relation
to app/javascript/app/views/index.js.cofee
to add the relation extension
class App.Project extends Spine.Model
@configure 'Project', 'name'
@extend Spine.Model.Ajax
@hasMany 'pages', 'projects/page'
class App.Page extends Spine.Model
@configure 'Page', 'name', 'content'
@extend Spine.Model.Ajax
@belongsTo 'project', 'Project'
in javascript console
App.Project.find(the_id).pages().create({name:"asd"})
more info in http://spinejs.com/docs/relations
the link is at the bottom of spinejs model documentation
I have a solution:
http://groups.google.com/group/spinejs/browse_thread/thread/6a5327cdb8afdc69?tvc=2
https://github.com/SpoBo/spine
I made a fork which overrides how url's are generated in the ajax module. This allows the create url to contain bits of the model's instance data. For example: /articles/1/comments. Works with create, update, etc.
class App.Post extends Spine.Model
@configure 'Post', 'channel_id', 'title'
@extend Spine.Model.Ajax
resourceUrl: ->
"channels/#{@channel_id}/posts"
@hasOne 'channel', 'App.Channel'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With