New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The Model can accept the scope to do the url scoping, especially useful for nested resources. #410
Conversation
either be a function or string. The generated url will be "scope/resources"
+1 I currently have to hack |
I think this looks like something that could be very useful. In the pull request it appears this is applied only at the model/collection level. Should we take this a step further and allow instances to also define their own scope? Seems like that would solve problems like #412. In this case the user wants to have an ajax request that looks like
Or perhaps somebody wants to use the same spine model for both normal
any thoughts/opinions? :o) |
My first thought on instance score assignment is that it would be an odd api design to require resources with different flags to use different paths, but then I realize that we have a couple odd cases like that. For example a resource moves to a state where it is not normally able to be updated, but we have one property we still want to be able to update so we made special API path for that one task... maybe that is bad API design... it our case simply added a method for sending off to that path rather than using a simple save, not a big deal. Wow, all that to say spine should probably encourage good API design, rather than catering to bad ones. The change as a way to define scope for related models seems like a pretty good idea. The 'take it a step further' per instance redefinition idea seem like a less good one, but I am still open to it. |
I think this relates directly with my ticket, I actually ended up using a hack to get around this problem. I'd be glad to see some sort of model scope functionality this functionality built in. |
So right now, the way you guys are handling this is overriding the
|
Indeed, I have two kinds of scopes in my own fork, one is Collection based and another is Instance based. For example, @scope: ->
"/projects/#{current.project_id}"
scope: ->
"/projects/#{@project_id}" However, I found that all my cases I can just use the Collection based scope. And what's more, in order to use the Instance based scope, I have to duplicate the url generation instead of reusing the part in Extend part. Include =
ajax: -> new Singleton(this)
url: (args...) ->
args.unshift(encodeURIComponent(@id))
args.unshift(@className.toLowerCase() + 's')
scope = this.scope?() or this.scope
if scope?
scope = scope.substring(1) if scope.charAt(0) is '/'
args.unshift(scope)
args.unshift(Model.host)
args.join('/') So I'm not sure whether it's a good idea to have two different methods. Thoughts? |
It does seem that overriding the Maybe there is a way to tweak the |
@cengebretson The current implementation is similar like what you suggest as the url() method in instance only append the resource id. The problem is that if the instance scope is different than model scope, we still need to define two scope or other dynamic path even though it's expected. However, we can pass the scope parameter in the instance url() to let the Collection version handle most of the logic. For example,
I have one version of such patch. It's not elegant but works. |
Seems like it might be worth investing in the underlying implementation a bit. |
@aeischeid Yup. Any suggestions? I can work on it and update the PR. Thanks. |
Sorry, I don't have anything insightful on how exactly to go about the refactoring of the underlying url bits. Feels like this discussion has nailed down the problem a little better, so at least you've got that to start from. ;) |
The rules to apply the scopes are: 1. If instance scope defined, the instance url will apply that scope. 2. If instance scope not defined, the instance url will apply the model scope if there is. 3. The model url will only apply the model scope if defined.
Scope can apply to collection/model level and object/instance level. The rules to apply the scope are:
|
Appreciate your work on this. Just to keep you updated we want to get this feature in but just haven't gotten around to it yet. |
Thanks for the update. |
The scope can either be a function or string. The generated url will be "scope/resources".
For example,