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

Commit 31440b06 by Sascha Depold

merge

2 parents 18d8ca6e 6ae09c0c
...@@ -36,9 +36,18 @@ module.exports = (function() { ...@@ -36,9 +36,18 @@ module.exports = (function() {
var self = this var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))) , accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName)))
obj[accessor] = function() { obj[accessor] = function(params) {
var id = this[self.identifier] var id = this[self.identifier]
return self.target.find(id)
if (!Utils._.isUndefined(params)) {
if (!Utils._.isUndefined(params.attributes)) {
params = Utils._.extend({where: {id:id}}, params)
}
} else {
params = id
}
return self.target.find(params)
} }
return this return this
......
...@@ -40,12 +40,21 @@ module.exports = (function() { ...@@ -40,12 +40,21 @@ module.exports = (function() {
HasOne.prototype.injectGetter = function(obj) { HasOne.prototype.injectGetter = function(obj) {
var self = this var self = this
obj[this.accessors.get] = function() { obj[this.accessors.get] = function(params) {
var id = this.id var id = this.id
, where = {} , where = {}
where[self.identifier] = id where[self.identifier] = id
return self.target.find({where: where})
if (!Utils._.isUndefined(params)) {
if (!Utils._.isUndefined(params.attributes)) {
params = Utils._.extend({where: where}, params)
}
} else {
params = {where: where}
}
return self.target.find(params)
} }
return this return this
......
...@@ -87,11 +87,15 @@ describe('HasMany', function() { ...@@ -87,11 +87,15 @@ describe('HasMany', function() {
user.setTasks([task1, task2]).success(function() { user.setTasks([task1, task2]).success(function() {
user.getTasks().success(function(tasks) { user.getTasks().success(function(tasks) {
expect(tasks.length).toEqual(2) expect(tasks.length).toEqual(2)
user.getTasks({attributes: ['title']}).success(function(tasks) {
expect(tasks[0].selectedValues.title).toEqual('task1')
expect(tasks[0].selectedValues.id).toEqual(null)
done() done()
}) })
}) })
}) })
}) })
})
it("should allow selfAssociation to be single linked (only one DAO is created)", function() { it("should allow selfAssociation to be single linked (only one DAO is created)", function() {
var oldLength = sequelize.daoFactoryManager.daos.length; var oldLength = sequelize.daoFactoryManager.daos.length;
......
...@@ -88,11 +88,15 @@ describe('HasOne', function() { ...@@ -88,11 +88,15 @@ describe('HasOne', function() {
user.setTask(task).on('success', function() { user.setTask(task).on('success', function() {
user.getTask().on('success', function(task2) { user.getTask().on('success', function(task2) {
expect(task.title).toEqual(task2.title) expect(task.title).toEqual(task2.title)
user.getTask({attributes: ['title']}).on('success', function(task2) {
expect(task2.selectedValues.title).toEqual('snafu')
expect(task2.selectedValues.id).toEqual(null)
done() done()
}) })
}) })
}) })
}) })
})
it("unsets unassociated objects", function() { it("unsets unassociated objects", function() {
var user, task1, task2; var user, task1, task2;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!