issue when passing argument to a function with `myarguments...` in coffeescript

180 Views Asked by At

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.

1

There are 1 best solutions below

5
On

When you call

Search.init

that invokes the Module.init method, defined in Spine like so:

Module.init = Controller.init = Model.init = (a1, a2, a3, a4, a5) ->
  new this(a1, a2, a3, a4, a5)

(Note that Spine.Class = Module.) That then invokes the Search constructor inherited from Module, which forwards all those arguments to the init method that Search.include attached to the Search prototype:

constructor: ->
  @init?(arguments...)

Hence the four undefined arguments (originally a2, a3, a4, and a4).