i have a search model like this (got this from https://github.com/maccman/quora2)
Spine = require('spine')
classMethods =
query: (params) ->
@select (rec) ->
rec.query params
instanceMethods =
query: (params) ->
attributes = @query_atts?() || @attributes()
for key of attributes
value = attributes[key]
if value?.query?(params).length
return true
else if value && typeof value == "string"
value = value.toLowerCase()
return true if value.indexOf(params) != -1
return false
class Search extends Spine.Class
@include Spine.Events
Search.Model =
extended: ->
@extend classMethods
@include instanceMethods
Search.include
init: (models...) ->
console.log('models: ', models)
@models = models
@results = []
query: (params) ->
@clear()
return unless params
@params = params.toLowerCase()
@models.forEach (model) =>
console.log(typeof(model))
continue until typeof(model) == 'undefined'
results = model.query(@params)
@results = @results.concat(results)
@trigger "change"
clear: ->
@results = []
@trigger "change"
each: (callback) ->
@results.forEach(callback)
module.exports = Search
and a Page model:
Spine = require('spine')
class Page extends Spine.Model
@configure 'Page', 'title', 'description'
@extend require('models/search').Model
constructor: ->
super
module.exports = Page
the issue i have is that whenever is instantiate a Search in my controller:
Search.init(Page)
and log what init gets as arguments i have the Page as a first argument and then i have 4 undefined.
models: [Page(title, description), undefined, undefined, undefined, undefined]
where do these come from?
of course it works fine in quora and it would work fine if i would not use models...
in the init.
When you call
that invokes the
Module.init
method, defined in Spine like so:(Note that
Spine.Class
=Module
.) That then invokes theSearch
constructor inherited fromModule
, which forwards all those arguments to theinit
method thatSearch.include
attached to theSearch
prototype:Hence the four undefined arguments (originally
a2
,a3
,a4
, anda4
).