Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var mongoose = require('mongoose'),
- Schema = mongoose.Schema,
- Tag = require('./tag.model'); //another mongoose Model
- var NoteSchema = new Schema({
- name: String,
- info: String,
- active: Boolean,
- tags: [{ type: String, ref: 'Tag' }],
- });
- NoteSchema.virtual('tagsAsString').get(function () {
- var result = "";
- for (var x in this.tags) {
- result += this.tags[x].name + " ";
- }
- return result;
- });
- NoteSchema.virtual('tagsAsString').set(function (data) {
- var split = data.split(' ');
- this.tags = new Array();
- for (var s in split) {
- Tag.find({ name: split[s] }).exec(function (err, tag) {
- if (tag == undefined || tag == 0 || tag.length == 0) {
- tag = new Tag({ name: split[s] });
- tag.save(function (err) {
- if (err) console.error(err);
- });
- this.tags.push(tag.name);
- }
- else {
- this.tags.push(tag.name);
- }
- this.save(function (err) {
- if (err) console.error(err);
- });
- });
- }
- });
- module.exports = mongoose.model('Note', NoteSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement