简体   繁体   English

如何将变量传递给Kraken.js模型?

[英]How to pass a variable into a Kraken.js model?

I have a variable that holds my Sequelize connection, like so: 我有一个保存我的Sequelize连接的变量,如下所示:

var sequelize = new Sequelize('database', 'username');

With Kraken.js, how do I pass this into a module? 使用Kraken.js,如何将其传递到模块中? I don't see where I could add this in the index.js configuration file... 我看不到可以在index.js配置文件中添加的位置...

Thanks! 谢谢!

(I can't tag this as krakenjs because I don't have enough karma.) (我无法将其标记为krakenjs,因为我没有足够的业力。)

It's a pretty similar setup to what's shown in this guide: 这与本指南中显示的设置非常相似:

http://sequelizejs.com/articles/express http://sequelizejs.com/articles/express

You would create the model file, (under ./models ). 您将创建模型文件(在./models下)。 You'd need to register this model using the associate function (See previous link). 您需要使用associate函数注册该模型(请参见上一链接)。

And to actually use the model, you could replicate the same usage as in the Kraken Shopping cart example https://github.com/lmarkus/Kraken_Example_Shopping_Cart/ 要实际使用该模型,您可以复制与Kraken Shopping购物车示例https://github.com/lmarkus/Kraken_Example_Shopping_Cart/中相同的用法

eg: (After you've registered your models) 例如:(注册模型后)

'use strict';
var Product = require('../models/productModel');

module.exports = function (server) {

 /**
* Display a list of the products.
*/
    server.get('/', function (req, res) {

        Product.find(function (err, prods) {

            res.render('index', products);    
        });   
    });
};

You can add the connection in separate file and export it ( http://openmymind.net/2012/2/3/Node-Require-and-Exports/ ). 您可以将连接添加到单独的文件中,然后将其导出( http://openmymind.net/2012/2/3/Node-Require-and-Exports/ )。 I store such files in a dir called lib/ 我将此类文件存储在名为lib /的目录中

Then in the index.js (the starting point of the app) import the file using 然后在index.js(应用程序的起点)中使用

require("./lib/.."). 

And then invoke it in index.js as 然后在index.js中以

app.configure = function configure(nconf, next) {...};

( http://krakenjs.com/#structure_of_a_project ) http://krakenjs.com/#structure_of_a_project

A good sample app is https://github.com/lmarkus/Kraken_Example_Shopping_Cart 一个很好的示例应用程序是https://github.com/lmarkus/Kraken_Example_Shopping_Cart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM