Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can SpineJS url() support Nested Resource in Rails?

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,

like image 691
Daniel Lv Avatar asked Dec 14 '11 14:12

Daniel Lv


3 Answers

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.

like image 144
Dane O'Connor Avatar answered Sep 30 '22 04:09

Dane O'Connor


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

like image 45
wizztjh Avatar answered Sep 30 '22 04:09

wizztjh


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' 
like image 22
SpoBo Avatar answered Sep 30 '22 02:09

SpoBo