How to expose User.id in JSON response from Loopback - angularjs

I have a simple use case that for the life of me I can't seem to figure out:
I have a model "Employee" that is based on the Loopback "User" model. My assumption is that the "id" of "Employee" would be the same as "User".
For the purpose of passing an Employee/User.id to an Angular.js route, I need to expose the Employee model's 'id' property in the RESTful response of the my Loopback API. Any idea on where or how to do this?
So far the Employee.id is profiled with the string "objectid"--which of course is not going to give me the correct user when I perform a Employee.find().

User.id is provided by LoopBack out-of-box. You can test this by adding a dummy user (via a boot script) and then browsing to localhost:3000/explorer to inspect the output of any GET request on the User object.

So, upon a more thorough review of the actual data in the Mongodb database it appears that '_id' in both User and Employee tables were always being populated with "objectid". This made me look at my models more closely. I compared them to the loopback-angular-admin's models--specifically their custom "user" model. What I noticed was that I had '"idInjection": true' on mine whereas the loopback-angular-admin project did not. So, I removed this property and VOILA: BSON/valid ObjectIds!
My models were generated with 'slc loopback:model'. This command line appears to add the '"idInjection":true' property to the models it generates. I don't know why this particular property would be problematic for Mongodb but the source of my original issue has been resolved.

Related

When updating a model on a RESTful API, should there be an update endpoint per field? or one endpoint for the model?

For a RESTful API, consider a model schema as follows:
MyCoolObject {
field_a
field_b
field_c
}
Is it better to create one update endpoint to update one or many fields on the model (PUT)? Or create one endpoint per field that would only update that one field (PATCH)?
Heuristic: how do you GET the information from your API?
Typically if you get the information a single resource with all of the information included in its representation...
GET /my-cool-object
Then you should also edit that information using the same resource
PUT /my-cool-object
PATCH /my-cool-object
POST /my-cool-object
In cases where you get the information from multiple resources (presumably via links)
GET /my-cool-object
GET /my-cool-object/a
GET /my-cool-object/b
GET /my-cool-object/c
Then you would normally edit the information in its own resource
PUT /my-cool-object/a
PATCH /my-cool-object/a
POST /my-cool-object/a

SuiteCommerce Advanced - Show a custom record on the PDP

I am looking to create a feature whereby a User can download any available documents related to the item from a tab on the PDP.
So far I have created a custom record called Documentation (customrecord_documentation) containing the following fields:
Related item : custrecord_documentation_related_item
Type : custrecord_documentation_type
Document : custrecord_documentation_document
Description : custrecord_documentation_description
Related Item ID : custrecord_documentation_related_item_id
The functionality works fine on the backend of NetSuite where I can assign documents to an Inventory item. The stumbling block is trying to fetch the data to the front end of the SCA webstore.
Any help on the above would be much appreciated.
I've come at this a number of ways.
One way is to create a Suitelet that returns JSON of the document names and urls. The urls can be the real Netsuite urls or they can be the urls of your suitelet where you set up the suitelet to return the doc when accessed with action=doc&id=_docid_ query params.
Add a target <div id="relatedDocs"></div> to the item_details.tpl
In your ItemDetailsView's init_Plugins add
$.getJSON('app/site/hosting/scriptlet.nl...?action=availabledoc').
then(function(data){
var asHtml = format(data); //however you like
$("#relatedDocs").html(asHtml);
});
You can also go the whole module route. If you created a third party module DocsView then you would add DocsView as a child view to ItemDetailsView.
That's a little more involved so try the option above first to see if it fits your needs. The nice thing is you can just about ignore Backbone with this approach. You can make this a little more portable by using a service.ss instead of the suitelet. You can create your own ssp app for the function so you don't have to deal with SCAs url structure.
It's been a while, but you should be able to access the JSON data from within the related Backbone View class. From there, within the return context, output the value you're wanting to the PDP. Hopefully you're extending the original class and not overwriting / altering the core code :P.
The model associated with the PDP should hold all the JSON data you're looking for. Model.get('...') sort of syntax.
I'd recommend against Suitelets for this, as that's extra execution time, and is a bit slower.
I'm sure you know, but you need to set the documents to be available as public as well.
Hope this helps, thanks.

CakePHP 3 - How to identify the installed behaviors in a model when baking a controller

I'd like to adapt my controller baking code (vendor/cakephp/bake/src/Template/Bake/Template/Bake/Controller/controller.ctp) so when baking a controller it will automatically detect if there is a (f.e. Translate-) behavior installed in the model and add "use Cake\I18n\I18n;" to the controller while baking it.
So, can anyone tell me how to identify the installed behaviors within the controller-baking-code?
Given that the table class already exists when baking the controller, you should be able to get the required information from $modelObj that is passed to the view, it's an instance of the table class that is associated with the controller.
The behavior registry available via Table::behaviors() should have the info that you need.
$modelObj->behaviors()->has('Translate')
And of course you can get further information from the table, like the schema (Table::schema()), validation rules (Table::validator()), etc...
See also
API > \Cake\ORM\Table::behaviors()
API > \Cake\ORM\BehaviorRegistry::has()

How to identify a BackboneJS Model serverside

I'm just getting my head around BackboneJS, but one of the (many) things I'm still struggling with is how exactly the Models Sync up and relate to the serverside DB records.
For example, I have a Model "Dvd", now I change an attribute on the "Dvd" Model, the Name for example, and then I call save(), how exactly does the server side know what DB record to update? Does Backbone hold the DB row ID or something else?
Yes, typically you will set up your Backbone models so that they have database row IDs for any objects you're working with. When one of them is brand new on the client side and not yet saved to the server, it will either have a default or no ID, which doesn't matter since the server will be assigning the ID if and when the initial save transaction succeeds.
Upon saving or updating a model item, Backbone expects the server to reply with some JSON that includes any attributes that have changed since the save or update request was made. In the response to the initial save request, the server informs the client of a newly saved item's row ID (and you can also send along any other information you might need to pass to the client at the same time).
By default, the 'id' attribute of a model object is assumed to be its unique identifier, but backbone lets you change this if you're using a different identifier for the primary key. Just give your model an idAttribute parameter (see the docs for Backbone.Model.extend()) to do that.
Meanwhile, either the urlRoot parameter or a url function can be given to your models to characterize the urls that should be used to send the various ajax requests to the server for a given model.

How to get $settings data out of CakePHP 2.0 FormAuthenticate object

I am building a RememberMe Component using the AuthComponent and would like to get the BaseAuthenticate::$settings data (userModel and fields) data out of the XxxxAuthenticate object so I can know what model and fields I should be dealing with, but I can't seem to figure out how to get that data back out.
Any suggestions?
Basically I need something with the same functionality as Auth::getModel( ) or Auth::$userModel from Cake 1.X.
Cake- 2.0.3
Auth::$userModel still exists in 2.0. However, you probably have to access it via the instantied object rather than statically:
$modelData = $this->Auth->userModel;
If this isn't set, then it defaults to User.
You can then get the model by looking at the first array key that is returned:
$modelName = key($modelData[0]);

Resources