Can SpineJS url () support a nested resource in Rails?

Rails has nested resources for a while, and it has been heavily used (or overused). Say we have two models, an article and a comment.

class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article end 

Define a nested resource in routes.rb

 resources :articles do resources :comments end 

So, now we can list the comments for a specific article: http: // localhost: 3000 / articles / 1 / comments

But Spine can only make the url for the mail request to create an article and comment as follows:

 /articles /comments 

How to make a Spine URL for an Ajax request as follows?

 /articles/1/comments 

I know that I can override url () in the comment model for comments, but what about creating a new post?

I am also looking at the source code, I found that the create () method in the Spine Ajax module does not care about the user-defined url () function in the Comment instance. I want to just pass the article_id and use it with my custom url () function to generate the url, after which I can send to the server to create.

Is this possible without fork and a modified version of Spine for mine?

btw: sorry for my english, wish all of you guys to understand what I want to say :-)

Thanks and best regards,

+7
source share
5 answers

The url property of a model can be a value or function. So you can 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 a resource endpoint when creating requests.

+2
source

add

 #= require spine/relation 

in app / javascript / app / views / index.js.cofee

to add a relationship 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"}) 

Additional information at http://spinejs.com/docs/relations

the link is at the bottom of the spinejs model documentation

+2
source

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 that overrides how the url is generated in the ajax module. This allows you to create a URL containing the data bits of the model instance. For example: / articles / 1 / comments. Works with creating, updating, 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' 
+2
source

I have the same problem as it seems to be one of the biggest problems with Spine.

It's quite simple to implement with BackBone due to its simplicity, but the internals of Spine are quite complex, which makes it quite complex.

I am trying to implement a stand, will return if I have a solution.

0
source

The next bit of code worked for me, BUT then it wasn’t ... (and I changed this answer). So this is NOT a good solution because Ajax requests are queued by Spine. It always worked for the first comment, but not for subsequent calls, as the call was super returned without sending PUT / POST.

 class Comment extends Spine.Model @configure "comment", "article_id" @extend Spine.Model.Ajax # hack for nested: @article_id is only set correctly during save @article_id = "?" @url: =>"/articles/#{@article_id}/comments" save: ()=> # hack for nested resources (see @url) Comment.article_id = @article_id super Comment.article_id = "?" 
0
source

All Articles