totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

is there way to reuse schema workflow? #699

Closed luasenvy closed 5 years ago

luasenvy commented 5 years ago

can i reuse schema workflow?

for example

schema/Example:

NEWSCHEMA('Example', function(){

  schema.setGet(async function($){
    $.callback({ body: '<div></div>' });
  });

  schema.setQuery(async function($){
    let example = await this.$get();
    $.callback(example);
  });

  schema.addWorkflow('get-other-type', async function($){
    let example = await this.$query();

    $.controller.header('Content-Type', 'text/html');
    $.callback(example.body);
  });
});

controller/example.js

ROUTE('GET /api/example',       ['*Example --> @get']  );
ROUTE('GET /api/examples',      ['*Example --> @query']);
ROUTE('GET /api/example-other', ['*Example --> @get-other-type']);
petersirka commented 5 years ago

This is very strange example. I don't recommend to use mixing schemas ... you can use chaining for schema operation like this:

ROUTE('GET /api/example        *Example --> @get @query @get-other-type (response)');

So the answer: Your example won't work. You need to know that Schema operation like setGet(), setQuery(), addWorkflow() works only with schema instance. What is the schema instance? It's an object generating according to the schema, this type of object contains a meta data about the schema.

var model = $CREATE('Example');

// Now model is SchemaIntance
model.$get(...)
model.$save(...)
model.$insert(...)
model.$update(...)
model.$workflow('....');

Back to your example:

schema.setGet(async function($){
    var model = $.model;
    // model is Schema Instance

    // So you can perform:
    // model.$workflow('..')
    // model.$save();
    // model.$query()

    // But now you want to rewrite "model" with a new object
    // So another operation won't know about the schema because you rewrite it
    $.callback({ body: '<div></div>' });

    // Possible solution:
    model.data = { body: '<div></div>' };
    $.callback(model);
});
luasenvy commented 5 years ago

thank you for reply.

I just wanted to reuse it without separating it into a function like the DAO in the MVC framework.

my curiosity was resolved. so close it :)