app/assets/javascripts/joosy/core/resource/rest.js.coffee in joosy-1.0.0.RC4 vs app/assets/javascripts/joosy/core/resource/rest.js.coffee in joosy-1.0.0.RC5
- old
+ new
@@ -10,10 +10,26 @@
__collection: ->
named = @__entityName.camelize().pluralize() + 'Collection'
if window[named] then window[named] else Joosy.Resource.RESTCollection
#
+ # Builds parents part of member path based on parents array
+ #
+ # @param [Array] parents Array of parents
+ #
+ # @example Basic usage
+ # Resource.__parentsPath([otherResource, '/bars/1']) # /other_resources/1/bars/1
+ #
+ @__parentsPath: (parents) ->
+ parents.reduce (path, parent) ->
+ path += if Joosy.Module.hasAncestor parent.constructor, Joosy.Resource.REST
+ parent.memberPath()
+ else
+ parent
+ , ''
+
+ #
# Builds member path based on the given id.
#
# @param [String] id ID of entity to build member path for
# @param [Hash] options See {Joosy.Resource.REST.find} for possible options
#
@@ -23,14 +39,12 @@
@memberPath: (id, options={}) ->
path = ("/" + @::__entityName.pluralize())
path = @__source if @__source? && !options.parent?
path += "/#{id}"
- if options.parent instanceof Joosy.Resource.Generic
- path = options.parent.memberPath() + path
- else if options.parent?
- path = options.parent + path
+ if options.parent?
+ path = @__parentsPath(if Object.isArray(options.parent) then options.parent else [options.parent]) + path
path += "/#{options.from}" if options.from?
path
#
@@ -54,14 +68,12 @@
#
@collectionPath: (options={}) ->
path = ("/" + @::__entityName.pluralize())
path = @__source if @__source? && !options.parent?
- if options.parent instanceof Joosy.Resource.Generic
- path = options.parent.memberPath() + path
- else if options.parent?
- path = options.parent + path
+ if options.parent?
+ path = @__parentsPath(if Object.isArray(options.parent) then options.parent else [options.parent]) + path
path += "/#{options.from}" if options.from?
path
#
@@ -179,14 +191,14 @@
#
# Requests the required resources from backend
#
# @param [String] where Possible values: 'all', id.
# 'all' will query for collection from collectionPath.
- # Everything else will be considered as an id string and will make resource
+ # Everything else will be considered as an id string and will make resource
# query for single instance from memberPath.
# @param [Hash] options Path modification options
- # @param [Function] callback Resulting callback
+ # @param [Function] callback Resulting callback
# (will receive retrieved Collection/Resource)
#
# @option options [Joosy.Resource.REST] parent Sets the given resource as a base path
# i.e. /parents/1/resources
# @option options [String] parent Sets the given staring as a base path
@@ -203,16 +215,16 @@
if where == 'all'
result = new (@::__collection()) this, options
@__query @collectionPath(options), 'GET', options.params, (data) =>
result.load data
- callback?(result)
+ callback?(result, data)
else
result = @build where
@__query @memberPath(where, options), 'GET', options.params, (data) =>
result.load data
- callback?(result)
+ callback?(result, data)
result
@__query: (path, method, params, callback) ->
options =
@@ -230,15 +242,15 @@
#
# Refetches the data from backend and triggers `changed`
#
# @param [Hash] options See {Joosy.Resource.REST.find} for possible options
- # @param [Function] callback Resulting callback
+ # @param [Function] callback Resulting callback
#
reload: (options={}, callback=false) ->
if Object.isFunction(options)
callback = options
options = {}
@constructor.__query @memberPath(options), 'GET', options.params, (data) =>
@load data
- callback? this
\ No newline at end of file
+ callback? this