【问题标题】:Create my own Model with node who allow new Model and Model.find()使用允许 new Model 和 Model.find() 的节点创建我自己的模型
【发布时间】:2015-04-23 06:50:45
【问题描述】:

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

需要模型

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,新的 Token({ /* 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;

对象

当我尝试使用一个对象时,find 函数运行良好,但我无法将它与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, []);
  }

};

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

感谢您的帮助!

【问题讨论】:

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


    【解决方案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 来执行此操作。不过我更喜欢我的例子。

    【讨论】:

    • 如果我添加其他方法(如`YourModule.prototype.save = function`)。 find 上的返回对象可以访问save() 函数吗? (每一个独立?)
    • 你必须在承包商内部定义一些属性,是的,我不知道你到底想用这个模型做什么。这个模型是 db 模型吗?是你在做的东西吗?保存功能有什么作用?
    • 但是,我不明白,如果find 函数返回多个对象。保存功能如何知道保存哪一个?
    • 亚瑟。我不知道您的查找功能是做什么的.. 或者您正在制作的这个模型是什么。详细说明您的模型。
    • 好的.. 对不起,英语不是我的母语.. 我已经更新了我的帖子以添加示例如何使用它。
    【解决方案2】:

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

    ./models/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));
      });
    };
    

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

    ./controllers/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");
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      相关资源
      最近更新 更多