使用允许新模型和Model.find()的节点创建自己的模型

时间:2015-02-21 17:04:20

标签: javascript node.js backbone.js model-view-controller model

我尝试使用节点创建模型,我希望能够像这样使用它:

需要模型

var Example = require('./models/example');

创建新对象

模型可以使用new

创建新对象
var example = new Example({ data:'example' });

查找

模型可以使用方法找到对象

Example.find({ data: 'mongoQuery' }, function(err, examples) {});

保存

该模型可以帮助找到使用方法返回对象的对象。

Example.findOne({ query: 'example' }, function(err, example) {
  example.set({ data: 'another data' });
  example.save();
});

使用示例

我希望能够使用这样的模型创建,查找(一个或多个),上传和删除令牌(例如)。我会在控制器或lib上使用它。例如。

var Token = require('./models/token); 

// Create and save a new token
var new_token = new Token({ key: 'abcdef' });
new_token.save();

// find multiple tokens, update, and save
var token = Token.find({key: 'abcderf'}, function(tokens) {
  for(var i in tokens) {
    tokens[i].set({key: '123456'});
    tokens[i].save();
  }
});

我已经尝试了很多东西,但是我无法创建一个同时允许new ExampleExample.find()的模块。

骨干:

我尝试使用Backbone,新的令牌({/ * attributes * /})可以正常工作但是find函数会返回错误:TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'find'

var Token = Backbone.Model.extend({

  initialize: function(attributes) {
    console.log("Token create");
    console.log(" attributes : ", attributes);
  },

  find : function(query, callback) {
    console.log("Token find");
    console.log(" query : ", query);
    callback(null, [ /* tokens */]);
  }
});
module.exports = Token;

对象

当我尝试使用对象时,查找功能运行良好,但我无法使用new返回错误:TypeError: object is not a function

module.exports = {

  find : function(query, callback) {
    console.log("[Step] models/token.js - find");
    console.log(" query : ", query);
    callback(null, []);
  }

};

创建模型以处理控制器上所有对象的最佳方法是什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

你走了:

YourModule = function(data){
    this.data = data; // this.data is now a property of the object you create with New. 
};

YourModule.prototype.find = function(query, callback) {
    console.log("[Step] models/token.js - find");
    console.log(" query : ", query);
    callback(null, []);
    return this.data; // this.data is available here.
};

YourModule.prototype.save = function(data) {
    this.data = data; // this.data is available here to.
};


module.exports = YourModule;

您无法使用new关键字实例化对象,但您可以Object.create执行此操作。我更喜欢我的例子。

答案 1 :(得分:0)

所以...我在@Neta Meta回复中创建了一个模型:

<强> ./模型/ token.js

var Backbone    = require('backbone');
var db          = require('../lib/database');

var Token = Backbone.Model.extend({
  initialize: function(attributes) {
  },

  save: function(callback) {
    db.collection('tokens').insert(self.attributes, function(err) {
      if (typeof callback === 'function') {
        callback(err, self);
      }
    });
  }
});


module.exports.create = function(attr, callback) {
   callback(null, new Token(attr));
};

module.exports.findOne = function(query, callback) {
  db.collection('tokens').findOne(query, function(err, result) {
    callback(err, new Token(result));
  });
};

所以现在我可以这样使用它:

<强> ./控制器/ example.js

var Token = require('../models/token');

// Create one :
Token.create({key: 'new data'}, function(err, token) {
  token.set({key: 'updated data'});
  token.save();
});

// Find one :
Token.findOne({key: 'updated data'}, function(err, token) {
  token.set({key: 're-updated data'});
  token.save(function(err) {
     console.log("Token saved");
  });
});