generator-angular-fullstack icon indicating copy to clipboard operation
generator-angular-fullstack copied to clipboard

Sharing object between backend and frontend in angular-fullstack env

Open luckylooke opened this issue 10 years ago • 5 comments

What is the best way to use same object for mongose schema and angular controler?

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var sharedObject = {
    name: String,
    info: String,
    active: Boolean
};
var MissionSchema = new Schema(sharedObject);

module.exports = mongoose.model('Mission', MissionSchema);

AND

angular.module('myApp')
  .controller('MyCtrl', function ($scope, $http, socket, sharingService) {
    var sharedObject = sharingService('myObjectSchema');

   generateForm(sharedObject);
  });

Any helpful info appreciated, thanks :)

luckylooke avatar Jul 16 '15 12:07 luckylooke

The newest versions of Mongoose include a way to share schema validation between client & server, but I haven't really looked into it yet. You might start there.

Awk34 avatar Jul 16 '15 18:07 Awk34

Thank you @Awk34, you pushed me closer. I have found this mongoose docs page. But still I cant see how I can extract Schema from mongoose object. :/

luckylooke avatar Jul 16 '15 19:07 luckylooke

The doc explains how to create Schema objects on the front-end. You'll need a way to include the duplicate data that's needed from the back-end & front-end. Maybe having a schemas folder that is used by both the server and client. It would be useful to include an example recipe of this in the generator/docs.

Awk34 avatar May 11 '16 19:05 Awk34

fantastick

srikanthgrvn avatar Feb 21 '17 11:02 srikanthgrvn

+1, I have the same question

chengtie avatar Dec 09 '17 20:12 chengtie