不要怂,就是干,撸起袖子干!

Commit 9fdc0b80 by Sascha Depold

add the eagerly loaded data to the toJSON and values method

1 parent d259cec7
......@@ -5,10 +5,12 @@ var Utils = require("./utils")
module.exports = (function() {
var DAO = function(values, options, isNewRecord) {
var self = this;
this.__options = options;
this.hasPrimaryKeys = options.hasPrimaryKeys;
this.selectedValues = values;
var self = this
this.__options = options
this.hasPrimaryKeys = options.hasPrimaryKeys
this.selectedValues = values
this.__eagerlyLoadedAssociations = []
initAttributes.call(this, values, isNewRecord)
......@@ -51,7 +53,7 @@ module.exports = (function() {
var result = {}
, self = this
this.attributes.forEach(function(attr) {
this.attributes.concat(this.__eagerlyLoadedAssociations).forEach(function(attr) {
result[attr] = self[attr]
})
......
......@@ -318,9 +318,14 @@ module.exports = (function() {
dao[accessor] = isEmpty ? null : daoInstance
} else {
dao[accessor] = dao[accessor] || []
if (! isEmpty)
if (!isEmpty) {
dao[accessor].push(daoInstance)
}
}
// add the accessor to the eagerly loaded associations array
dao.__eagerlyLoadedAssociations = Utils._.uniq(dao.__eagerlyLoadedAssociations.concat([accessor]))
})
}
......
......@@ -334,7 +334,6 @@ describe('DAO', function() {
})
})
})
})
})
})
......@@ -274,12 +274,14 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
username: Helpers.Sequelize.STRING,
age: Helpers.Sequelize.INTEGER,
isAdmin: Helpers.Sequelize.BOOLEAN
}, {
timestamps: false,
logging: true
})
}, { timestamps: false })
this.Project = this.sequelize.define('NiceProject', { title: Helpers.Sequelize.STRING }, { timestamps: false })
this.User.sync({ force: true }).success(done)
this.User.hasMany(this.Project, { as: 'Projects' })
this.Project.belongsTo(this.User, { as: 'LovelyUser' })
this.sequelize.sync({ force: true }).success(done)
})
it('returns an object containing all values', function() {
......@@ -296,6 +298,30 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
var user = this.User.build({ username: 'test.user', age: 99, isAdmin: true })
expect(JSON.parse(JSON.stringify(user))).toEqual({ username: 'test.user', age: 99, isAdmin: true, id: null })
})
it('includes the eagerly loaded associations', function(done) {
this.User.create({ username: 'fnord', age: 1, isAdmin: true }).success(function(user) {
this.Project.create({ title: 'fnord' }).success(function(project) {
user.setProjects([ project ]).success(function() {
this.User.findAll({include: [ { model: this.Project, as: 'Projects' } ]}).success(function(users) {
var _user = users[0]
expect(_user.projects).toBeDefined()
expect(JSON.parse(JSON.stringify(_user)).projects).toBeDefined()
this.Project.findAll({include: [ { model: this.User, as: 'LovelyUser' } ]}).success(function(projects) {
var _project = projects[0]
expect(_project.lovelyUser).toBeDefined()
expect(JSON.parse(JSON.stringify(_project)).lovelyUser).toBeDefined()
done()
})
}.bind(this))
}.bind(this))
}.bind(this))
}.bind(this))
})
})
describe('findAll', function findAll() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!