Skip to content

Commit

Permalink
Wingman now expects models to defined on Wingman.global which is equa…
Browse files Browse the repository at this point in the history
…l to window in browser environments.
  • Loading branch information
Rasmus Rønn Nielsen committed Apr 10, 2012
1 parent 9bc6fcb commit 3cd319a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
2 changes: 2 additions & 0 deletions lib/wingman.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,9 @@
if window? if window?
exports.document = window.document exports.document = window.document
exports.window = window exports.window = window
exports.global = window
exports.localStorage = localStorage exports.localStorage = localStorage

exports.request = require('./wingman/request') exports.request = require('./wingman/request')
exports.Template = require('./wingman/template') exports.Template = require('./wingman/template')
exports.View = require('./wingman/view') exports.View = require('./wingman/view')
Expand Down
4 changes: 1 addition & 3 deletions lib/wingman/model.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ module.exports = class Model extends WingmanObject
for hasManyName in @constructor.hasManyNames for hasManyName in @constructor.hasManyNames
klassName = Fleck.camelize(Fleck.singularize(Fleck.underscore(hasManyName)), true) klassName = Fleck.camelize(Fleck.singularize(Fleck.underscore(hasManyName)), true)


# Ideally, we should not require an app to be instantiated to find other model classes. klass = Wingman.global[klassName]
# But for now I cannot come up with a better solution than to find the model classes in the current apps constructor.
klass = Wingman.Application.instance.constructor[klassName]
association = new HasManyAssociation @, klass association = new HasManyAssociation @, klass
@setProperty hasManyName, association @setProperty hasManyName, association
association.bind 'add', (model) => @trigger "add:#{hasManyName}", model association.bind 'add', (model) => @trigger "add:#{hasManyName}", model
Expand Down
62 changes: 23 additions & 39 deletions test/cases/model_test.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ HasManyAssociation = require '../../lib/wingman/model/has_many_association'
sinon = require 'sinon' sinon = require 'sinon'
RestStorage = require '../../lib/wingman/model/storage_adapters/rest' RestStorage = require '../../lib/wingman/model/storage_adapters/rest'


uglyAssociationHack = (klass) ->
# For now we have no better solution than this
Wingman.Application.instance = { constructor: {} }
Wingman.Application.instance.constructor[klass.name] = klass

module.exports = class ModelTest extends Janitor.TestCase module.exports = class ModelTest extends Janitor.TestCase
setup: ->
Wingman.global = {}

teardown: ->
delete Wingman.global
delete Wingman.request.realRequest

'test setting attributes via constructor': -> 'test setting attributes via constructor': ->
User = class extends Wingman.Model User = class extends Wingman.Model
user = new User name: 'Rasmus', age: 25 user = new User name: 'Rasmus', age: 25
Expand Down Expand Up @@ -244,21 +246,19 @@ module.exports = class ModelTest extends Janitor.TestCase
class User extends Wingman.Model class User extends Wingman.Model
@hasMany 'notifications' @hasMany 'notifications'


class Notification extends Wingman.Model class Wingman.global.Notification extends Wingman.Model


uglyAssociationHack Notification
user = new User() user = new User()
@assertEqual Notification, user.get('notifications').associatedClass @assertEqual Wingman.global.Notification, user.get('notifications').associatedClass


'test initialization of has many association with a two word name': -> 'test initialization of has many association with a two word name': ->
class User extends Wingman.Model class User extends Wingman.Model
@hasMany 'forumTopics' @hasMany 'forumTopics'


class ForumTopic extends Wingman.Model class Wingman.global.ForumTopic extends Wingman.Model


uglyAssociationHack ForumTopic
user = new User() user = new User()
@assertEqual ForumTopic, user.get('forumTopics').associatedClass @assertEqual Wingman.global.ForumTopic, user.get('forumTopics').associatedClass


'test has many association count': -> 'test has many association count': ->
id = 1 id = 1
Expand All @@ -269,18 +269,15 @@ module.exports = class ModelTest extends Janitor.TestCase
class User extends Wingman.Model class User extends Wingman.Model
@hasMany 'notifications' @hasMany 'notifications'


class Notification extends Wingman.Model class Wingman.global.Notification extends Wingman.Model

uglyAssociationHack Notification


user = new User() user = new User()
user.save() user.save()


new Notification(userId: 1).save() for [1..2] new Wingman.global.Notification(userId: 1).save() for [1..2]
new Notification(userId: 2).save() new Wingman.global.Notification(userId: 2).save()


@assertEqual 2, user.get('notifications').count() @assertEqual 2, user.get('notifications').count()
delete Wingman.Application.instance


'test has many association add event': -> 'test has many association add event': ->
id = 1 id = 1
Expand All @@ -291,10 +288,7 @@ module.exports = class ModelTest extends Janitor.TestCase
class User extends Wingman.Model class User extends Wingman.Model
@hasMany 'notifications' @hasMany 'notifications'


class Notification extends Wingman.Model class Wingman.global.Notification extends Wingman.Model

# For now we have no better solution than
uglyAssociationHack Notification


context = new WingmanObject context = new WingmanObject
callbackValues = [] callbackValues = []
Expand All @@ -305,27 +299,24 @@ module.exports = class ModelTest extends Janitor.TestCase
context.set { user } context.set { user }


notifications = [ notifications = [
new Notification(userId: 1) new Wingman.global.Notification(userId: 1)
new Notification(userId: 2) new Wingman.global.Notification(userId: 2)
new Notification(userId: 1) new Wingman.global.Notification(userId: 1)
] ]
notification.save() for notification in notifications notification.save() for notification in notifications


@assertEqual 2, callbackValues.length @assertEqual 2, callbackValues.length
@assertEqual notifications[0], callbackValues[0] @assertEqual notifications[0], callbackValues[0]
@assertEqual notifications[2], callbackValues[1] @assertEqual notifications[2], callbackValues[1]
delete Wingman.Application.instance


'test has many nested population': -> 'test has many nested population': ->
class User extends Wingman.Model class User extends Wingman.Model
@hasMany 'notifications' @hasMany 'notifications'


class Notification extends Wingman.Model class Wingman.global.Notification extends Wingman.Model

uglyAssociationHack Notification


user = new User id: 1, name: 'Rasmus', notifications: [ { id: 1, title: 'yeah' }, { id: 2, title: 'something else' } ] user = new User id: 1, name: 'Rasmus', notifications: [ { id: 1, title: 'yeah' }, { id: 2, title: 'something else' } ]
@assertEqual 2, Notification.count() @assertEqual 2, Wingman.global.Notification.count()
@assertEqual 2, user.get('notifications').count() @assertEqual 2, user.get('notifications').count()


'test has many association with json export': -> 'test has many association with json export': ->
Expand All @@ -337,19 +328,15 @@ module.exports = class ModelTest extends Janitor.TestCase
class User extends Wingman.Model class User extends Wingman.Model
@hasMany 'notifications' @hasMany 'notifications'


class Notification extends Wingman.Model class Wingman.global.Notification extends Wingman.Model

uglyAssociationHack Notification


user = new User() user = new User()
user.save() user.save()


new Notification(userId: 1).save() for [1..2] new Wingman.global.Notification(userId: 1).save() for [1..2]
new Notification(userId: 2).save() new Wingman.global.Notification(userId: 2).save()


@assert !user.toJSON().notifications @assert !user.toJSON().notifications

delete Wingman.Application.instance


'test store add': -> 'test store add': ->
class User extends Wingman.Model class User extends Wingman.Model
Expand All @@ -367,8 +354,5 @@ module.exports = class ModelTest extends Janitor.TestCase
class User extends Wingman.Model class User extends Wingman.Model
user = new User id: 1 user = new User id: 1
@assertThrows -> user.set id: 2 @assertThrows -> user.set id: 2

teardown: ->
delete Wingman.request.realRequest


# TODO: LOAD AND DESTROY # TODO: LOAD AND DESTROY

0 comments on commit 3cd319a

Please sign in to comment.