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

Commit 6ae09c0c by joshm

Add ability to use attributes in belongsTo and hasOne accessors

This will allow you to put attributes into the accessor requests.
It also complements the newly added field selectedValues so you can get
the exact fields you requested when using associations.
1 parent 1b5385ac
......@@ -32,9 +32,16 @@ module.exports = (function() {
var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName)))
obj[accessor] = function() {
obj[accessor] = function(params) {
var id = obj[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
......
......@@ -37,12 +37,21 @@ module.exports = (function() {
HasOne.prototype.injectGetter = function(obj) {
var self = this
obj[this.accessors.get] = function() {
obj[this.accessors.get] = function(params) {
var id = obj.id
, where = {}
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
......
......@@ -87,11 +87,15 @@ describe('HasMany', function() {
user.setTasks([task1, task2]).success(function() {
user.getTasks().success(function(tasks) {
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()
})
})
})
})
})
it("should allow selfAssociation to be single linked (only one DAO is created)", function() {
var oldLength = sequelize.daoFactoryManager.daos.length;
......
......@@ -88,11 +88,15 @@ describe('HasOne', function() {
user.setTask(task).on('success', function() {
user.getTask().on('success', function(task2) {
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()
})
})
})
})
})
it("unsets unassociated objects", function() {
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!