使用MeanJS安装猫鼬朋友插件 [英] Installing mongoose friends plugin with MeanJS

查看:82
本文介绍了使用MeanJS安装猫鼬朋友插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的支持!

每次我为mean.js运行Yeoman CRUD支架时,都会破坏我的应用程序.页面变成白色.因此,我不确定要在哪个文件中添加CRUD功能.

Every time I run a Yeoman CRUD scaffold for mean.js, it breaks my app. The page turns completely white. So I'm not exactly sure which files to add where for CRUD functionality.

我希望用户能够像在Facebook上那样彼此成为朋友".有一个名为mongoose friends的插件.看起来非常有前途.问题是git存储库中没有包含示例html页面来引导我完成安装.看到有一个包含plugin.js和status.js的lib文件夹.是服务器端还是客户端?我猜它在服务器端...

I'd like the ability for users to "friend" each other like on Facebook. There's this plugin called mongoose friends. It looks very promising. The problem is there's no example html page included with the git repository to walk me through installation. Is see there's a lib folder with plugin.js and status.js. Does that go server side or client side? I'm guessing it goes server side...

https://github.com/numbers1311407/mongoose-friends

我如何将所有这些代码在单个js文件中分离到用于MeanJS的MVC中?

How would I separate all this code out in the single js file into MVC for MeanJS?

我最初不想发布此问题,因为我认为这个问题可能问得太多或太广泛,但是后来我意识到这是一个基本的如何安装插件?"问题.新的MeanJS用户可以从此类问题中受益.

I didn't want to post this question at first, because I thought the question might be asking too much or too broad, but then I realized it's a basic "How do I install a plugin?" question. New MeanJS users could benefit from this type of question.

推荐答案

通过npm即可轻松安装,

Installing is easy via npm, simply:

npm install mongoose-friends --save

我没有使用MEAN.JS的经验,但是在大多数情况下,这似乎是一组生成器,用于创建CRUD模式的angular/express应用程序.因此,似乎遵循框架的哲学来创建作为CRUD资源的友谊.

I have no experience with MEAN.JS, but it seems to be for the most part a collection of generators to create a CRUD-patterned angular/express app. As such it seems to follow the philosophy of the framework to create the friendship as a CRUD resource.

按照他们的建议使用yo:

yo meanjs:crud-module friendship

这将为友谊模型生成MVC,但会对模型本身做出一些不正确的假设,即它是一流的猫鼬模型.有了这个插件,事实并非如此.相反,友谊是用户记录上嵌入式集合的一部分,该插件为其提供了CRUD方法.

This will generate the MVC for a friendship model, but will make some incorrect assumptions about the model itself, namely that it is a first class mongoose model. With this plugin, it is not. Rather friendships are part of an embedded collection on the user record, the plugin provides CRUD methods for them.

首先,将插件添加到您的用户模型中.

First off, add the plugin to your user model.

// in app/models/user.server.model.js
var friends = require("mongoose-friends");
// ...
UserSchema.plugin(friends());

app/models/friendship处生成的模型以及在生成的文件中对其的引用将需要删除.代替Friendship模型,将通过添加到您的User模型中的插件方法对友谊进行CRUD.

The generated model at app/models/friendship, and references to it in the generated files, will need to be removed. Instead of a Friendship model, frienships will be CRUD'd through the plugin methods added to your User model.

app/controllers/friendships.server.controller.js处生成的控制器可能需要最多的更改.

The controller generated at app/controllers/friendships.server.controller.js will likely require the most change.

create,例如,将对此进行更改:

create, for example would change from this:

var friendship = new Friendship(req.body);
friendship.user = req.user;
friendship.save(callback);

更像是:

req.user.requestFriend(req.body.user, callback);

路由也可能需要更改,具体取决于您的应用程序如何使用友谊.插件的友谊不是一流的资源,而是用户的嵌入式集合.因此,例如,没有公共的/friendships路线.该路线仅需要返回已登录用户的朋友,或者您想要映射特定于该用户的友谊路线,例如/users/ID/friendships,如果用户的友谊可以被用户本身以外的其他人看到.

The routes may need to change as well, depending on how your application uses friendships. Friendships of the plugin are not a first-class resource, but rather an embedded collection of a user. As such there's no public /friendships route, for example. Either that route would need to return only the logged in users friends, or you'd want to map a friendship route specific to the user, e.g. /users/ID/friendships, in the case where the friendships of a user were viewable by those other than the user itself.

无论如何,这无疑是不完整的,甚至是被误导的,但是我希望这足以使您开始实施.

Anyway, this is no doubt woefully incomplete and perhaps even misguided, but I hope it's enough to get you started on the implementation.

这篇关于使用MeanJS安装猫鼬朋友插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆