#1 2017-12-21 04:46:15

Joker
Member
Registered: 2015-07-06
Posts: 15

Mustache expression helper - accessing object properties

Hi all,
I want to return an object via an expression helper and access properties of that object on the web page (rendered via Mustache). I don't know if it's supposed to work, but I certainly can't get it to work.

Below is an example of what I am trying to do.

Here is the object

  TSQLUserProduct = class(TSQLRecord)
  protected
    fName: String; 
    fBarCode: String; 
    fProductSize: Double; 
    fPurchUnits: TUseUnits; 
    fUseUnits: TUseUnits; 
  published
    property Name: String index 40 read fName write fName;
    property BarCode: String index 40 read fBarCode write fBarCode;
    property ProductSize: Double read fProductSize write fProductSize;
    property PurchUnits: TUseUnits read fPurchUnits write fPurchUnits;
    property UseUnits: TUseUnits read fUseUnits write fUseUnits;
  end;

The expression helper ...

procedure GetProduct (const Value: variant; out result: variant);
  var Product : TSQLUserProduct;
begin
  SetVariantNull(result);

  try
    .... get product based on ID ...
  
     _ObjAddProps (['Product', Product], Result);

   finally
    Product.Free;
  end;
end;

The web page ...

....

            <td>{{#GetProduct ProductID}}  {{Product.ProductSize}} {{Product.PurchUnits}} {{/GetProduct}}</td>

....

Last edited by Joker (2017-12-21 04:47:46)

Offline

#2 2017-12-21 08:10:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Mustache expression helper - accessing object properties

What does the result variant contain in your helper?
Why not just set

result := Product.GetSimpleFieldsAsDocVariant;

Then use it directly as

<td>{{#GetProduct ProductID}}  {{ProductSize}} {{PurchUnits}} {{/GetProduct}}</td>

Anyway, retrieving info as such in a helper is breaking one of the benefit of Mustache, which expect all the data to be already available in the data context.
Helpers are to modify the presentation/layout of data, to actually retrieve data.

Offline

#3 2017-12-21 21:28:36

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Mustache expression helper - accessing object properties

Thanks for the response AB, but I don't think this will work for me.
I should probably expand a little on what I am doing as it will make more sense why I am actually doing this even though it is a little against Mustache "rules".

This is the layout of the "system":

SQLite DB --- App Server (interface based REST server) --- FMX App (uses MormotClient.pas)
                             |
                              ---- Web Server (MVC server also using MormotClient.pas to communicate with the App Server)


So as you can see the web server does not have direct access the the TSQLUserProduct, it uses the type defined in MormotClient.pas.
To expand a little further, the TSQLUserProduct is actually a property of another object TSQLPurchProduct, and I am actually passing the TSQLPurchProduct to the context, however I also need access to the TSQLUserProduct property.

Here is the code from the App Server

  TSQLPurchProduct = class(TSQLRecord)
  private
    fProductID : TSQLUserProduct;
    fOnHand  : double;
    fPurchDate : TDateTime;
    fPurchasedFrom : RawUTF8;
    fCost : double;
...
  published
    property ProductID : TSQLUserProduct read fProductID write fProductID;
    property PurchDate : TDateTime read fPurchDate write fPurchDate;
    property PurchasedFrom : RawUTF8 read fPurchasedFrom write fPurchasedFrom;
    property Cost : double read fCost write fCost;
  end;

Now here is the code on the web server, where I am having the difficulty. As you can see the ProductID is defined as a TID rather than a TSQLUserProduct.

  TSQLPurchProduct = class(TSQLRecord)
  protected
    fProductID: TID; 
    fOnHand: Double; 
    fPurchDate: TDateTime; 
    fPurchasedFrom: String; 
    fCost: Double; 
  published
    // defined as ProductID: TSQLUserProduct on the server
    property ProductID: TID read fProductID write fProductID;
    property OnHand: Double read fOnHand write fOnHand;
    property PurchDate: TDateTime read fPurchDate write fPurchDate;
    property PurchasedFrom: String read fPurchasedFrom write fPurchasedFrom;
    property Cost: Double read fCost write fCost;
  end;

Last edited by Joker (2017-12-21 22:13:51)

Offline

#4 2017-12-21 22:11:40

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Mustache expression helper - accessing object properties

So I tried something along the lines of what you suggested using the SynCrossPlatformREST TSQLRecord.ToVariant method eg Product.ToVariant.

So the GetProduct helper returns {"ID":2,"Name":"Product2","BarCode":"1234","ProductSize":5,"PurchUnits":2,"UseUnits":1}

And I have this {{#GetProduct ProductID}} {{Name}} {{/GetProduct}} in my web page, but it does not render the name.

Last edited by Joker (2017-12-21 22:12:17)

Offline

#5 2017-12-21 22:16:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Mustache expression helper - accessing object properties

Check the data context by appending /json to the URI.

Offline

#6 2017-12-21 23:54:26

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Mustache expression helper - accessing object properties

No the "product" JSON does not appear in the data context when I append /json ... however I would not expect it to either, as the the helper function gets called after the context has been generated.

Offline

#7 2017-12-22 08:32:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Mustache expression helper - accessing object properties

Fair enough.

I don't know what his wrong in your code.

I don't remember well, but is {{#helper value}} syntax supported?
It was meant to be used only as {{helper value}}.

Offline

#8 2017-12-22 21:06:10

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Mustache expression helper - accessing object properties

According to the documentation using a section should work - https://synopse.info/files/html/Synopse … #TITLE_509

I think the issue is that although the object is returned by the expression helper it does not become part of the data context, so can't be referenced.

It would be nice if it could, but it's not the end of the world.


... and can I just say that I think the Mormot framework is absolutely fantastic and I really love using it.

Offline

Board footer

Powered by FluxBB