totaljs / framework

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

How to get 'parameters' from controller in model using GET? #636

Closed blaasvaer closed 6 years ago

blaasvaer commented 6 years ago

Can someone explain to me how I can access parameters from a model, passed to a route with AJAX?

I have this in the client:

...
return $.ajax({
    type: 'GET',
    url: '/admin/api/pages/' + id
...

And this in the controller:

...
ROUTE('/admin/api/pages/{id}', json_query, ['*Pages']);
...
function json_query ( id ) {
    if ( id ) {
        _this.$get( this.callback() ); // Can't pass 'id' in from here, as it would 'expect' to be a helper (whatever that is ... )
    }
...

And this in the model:

schema.setGet(function ($) {
    const sql = DATABASE( new ErrorBuilder() );

    sql.select('result', 'pages').make(function ( builder ) {
        builder.where('id', '=', id); <-- HOW do I get the id from the controller?
    });

    sql.exec(function ( err, response ) {
        $.callback( response.result );
    });
});

Whatever I try, I get no closer than this:

sqlagent/pg (552) ----- exec
sqlagent/pg (552) result SELECT * FROM pages WHERE "id"='='
sqlagent/pg (552) ----- done (42 ms)
molda commented 6 years ago

@blaasvaer Since id is most useful it's actualy implemented right into schemas

schema.setGet(function ($) {
    // $.controller.id contains a value of the first argument of dynamic routing
    $.controller.id;
...

another way is

...
ROUTE('/admin/api/pages/{id}', json_query, ['*Pages']);
...
function json_query ( id ) {
    if ( id ) {
                // pass id as a helper and use it in schema as $.options
        _this.$get(id,  this.callback() ); 
    }
}
...

model

schema.setGet(function ($) {
    $.options // the id
});
blaasvaer commented 6 years ago

Thanks.