From 31e3db6d24bec7ae846949c71d4f659815155e53 Mon Sep 17 00:00:00 2001 From: Florian Holzapfel Date: Fri, 24 May 2013 19:48:55 +0200 Subject: [PATCH] ADD: better handling of category types --- lib/category.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/category.js b/lib/category.js index 7311961..d5c8acd 100644 --- a/lib/category.js +++ b/lib/category.js @@ -26,15 +26,20 @@ var utils = require('./utils'); var category = function(client, xml) { this.client = client; if(xml) { + var get_type = function(type) { + return type; + }; + this.get_type = get_type.bind(this, xml['task-category'] ? 'task-category' : 'deal-category'); utils.parse_xml(this, xml['task-category'] || xml['deal-category'] || xml); } }; -category.prototype.create = function(type, item, callback) { - var path = '/' + type + '_categories.xml'; +category.prototype.create = function(item, callback) { + var path = '/' + item.type + '_categories.xml'; + delete item.type; this.client.create_item(path, utils.to_xml(item, type + '-category'), callback, category); }; -category.prototype.delete = function(type, callback) { - var url = '/' + type + '_categories/' + this.id + '.xml'; +category.prototype.delete = function(callback) { + var url = '/' + this.get_type() + '_categories/' + this.id + '.xml'; this.client.send('DELETE', url, function(err, res) { callback(err); @@ -43,11 +48,11 @@ category.prototype.delete = function(type, callback) { category.prototype.get = function(type, id, callback) { this.client.get_item('/' + type + '_categories/' + id + '.xml', callback, category); }; -category.prototype.to_xml = function(type, excluded_keys) { - return utils.to_xml(this, type + '-category', excluded_keys); +category.prototype.to_xml = function(excluded_keys) { + return utils.to_xml(this, this.get_type() + '-category', excluded_keys); }; -category.prototype.update = function(type, callback) { - var path = '/' + type + '_categories/' + this.id + '.xml'; +category.prototype.update = function(callback) { + var path = '/' + this.get_type() + '_categories/' + this.id + '.xml'; this.client.send('PUT', path, this.to_xml(type), callback); };