Querying relations in loopback doesn't work via Angular SDK - angularjs

I am trying to query a model relation
I have two models
1. Workers
2. Skills
Worker hasMany Skills
I can query the api via the explorer
http://localhost:3000/explorer/#!/Workers/prototype_get_skills
and /api/Worker/:id/Skills url
and it gives back a list of skills for a give Worker Id
The issue happens when i try to call the Worker.skills() method generated by Angular SDK where i get a 404 Not Found Error
Below is the Angular implementation that i have
angular.module('worker-dashboard').factory('WorkerDashboardSkillService',['Worker',
function(Worker){
function getWorkerSkills(worker){
return Worker.skills({
filter:
{
where:
{
"workerId" : worker
}
}
},function(data){
console.log(data);
},
function(err){
console.log(err);
})
}
function addWorkerSkills(worker,skill){
return Worker.skills.create(
{
"skillName": skill.name,
//TODO chabge below
"skillCategory": skill.name,
"workerId": worker
},function(data){
console.log(data);
},
function(err){
console.log(err);
})
}
return{
getWorkerSkills : getWorkerSkills,
addWorkerSkills : addWorkerSkills
}
}]);
I also tried an example loopback-getting-started-intermediate
Which has an example
$scope.reviews = Review.find({
filter: {
where: {
publisherId: $rootScope.currentUser.id
},
include: [
'coffeeShop',
'reviewer'
]
}
});
However this example is looks like for the belongsTo relation and when i tried modifying it couldn't do it
Edit : Adding Worker.json
{
"name": "Worker",
"base": "User",
"idInjection": true,
"properties": {
"workerName": {
"type": "string"
},
"workerFirstName": {
"type": "string"
},
"workerLastName": {
"type": "string"
},
"isWorkerBlackListed": {
"type": "boolean"
},
"workerBlacklistedByClient": {
"type": [
"string"
]
}
},
"validations": [],
"relations": {
"skills": {
"type": "hasMany",
"model": "Skills",
"foreignKey": "workerId"
}
},
"acls": [
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": []
}
Skills.json
{
"name": "Skills",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"skillName": {
"type": "string"
},
"skillCategory": {
"type": "string"
}
},
"validations": [],
"relations": {
"worker": {
"type": "belongsTo",
"model": "Worker",
"foreignKey": ""
}
},
"acls": [
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": []
}

Better late than never. I experienced the same trouble today. You just need to add the angular dependency.
angular
.module('app')
//inject all the model you need
.controller('ListBooksController', ['$scope', '$state', 'Book', 'Collection', 'Author', function($scope,
$state, Book, Collection, Author) {
$scope.books = Book.find({
filter: {
include: [
'author'
//put the model you want to retrieve
]
}
});
console.log($scope.books);
}])

Related

How to add Single-field Exemptions in Firestore Index Config File

Please let me know how to add Index exemptions for Single-field, in the firebase-indexes.json file, in order to deploy through CLI.
Currently, below is my index config in the file firebase-indexes.json, able to deploy through CLI, but it is creating an index of type Composite, not as a Single-field Exemption.
{
"indexes": [
{
"collectionGroup": "comments",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "id",
"order": "ASCENDING"
},
{
"fieldPath": "id",
"order": "DESCENDING"
}
]
}
]
}
Thanks in advance.
Assuming that your collection is called "comments" and your exemption field is called "field", you will add a new property to your firestore.indexes.json called "fieldOverrides", like this:
{
"indexes": [
// your indexes here
],
"fieldOverrides": [
{
"collectionGroup": "comments",
"fieldPath": "field",
"indexes": [
{
"order": "ASCENDING",
"queryScope": "COLLECTION"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION"
},
{
"order": "ASCENDING",
"queryScope": "COLLECTION_GROUP"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION_GROUP"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION_GROUP"
}
]
}
]
}

Country -> State-> City with angular-schema-form-dynamic-select

I am currently using angular-schema-form-dynamic-select and my requirement is to select states based on a country selected. I'm storing data in the db like this country -> state -> city. Can anyone Help me on this?
This is my form:
[
{
"key": "country",
"type": "strapselect",
"placeholder":"country",
"options": {
"httpGet": {
"url": "/countries"
},
"map": { "valueProperty": "readonlyProperties.id", "nameProperty":"name" }
}
},
{
"key": "state",
"type": "strapselect",
"placeholder":"state",
"options": {
"httpGet": {
"url": "/states"
},
"map": { "valueProperty": "readonlyProperties.id", "nameProperty":"name" }
}
},
{
"key": "city",
"type": "strapselect",
"placeholder":"city",
"options": {
"httpGet": {
"url": "/cities"
},
"map": { "valueProperty": "readonlyProperties.id", "nameProperty":"name" }
}
}
]
I think a feature like that would be indeed quite handy. Maybe you write something like this in the json string:
{
"type": "object",
"properties": {
"country": {
"type": "string",
"enumCallback": "getTitlesValues()"
}
}
}
And in your controller you would have that callback defined:
...
$scope.getTitlesValues = function () {
return ['India','Australia', 'Germany', 'Sweden']
}
...
I think a feature like that would be indeed quite handy.
Maybe you write something like this in the json string:
{
"type": "object",
"properties": {
"country": {
"type": "string",
"enumCallback": "getTitlesValues()"
}
}
}
And in your controller you would have that callback defined:
...
$scope.getTitlesValues = function () {
return ['India','Australia', 'Germany', 'Sweden']
}
...

Loopback - Extended AccessToken model not working

I'm using loopback's passport component with AngularJs frontend. I've created custom model for User as Person and for AccessToken as PersonAccessToken.
I'm able to login in correctly through facebook and redirect it to the angularjs client. But what I see is that access tokens are getting stored in built in AccessToken model rather than my custom PersonAccessToken model.
So when I call Person.logout() function it throws 500 server error since it cannot find any access tokens stored.
Person.json:
{
"name": "Person",
"base": "User",
"strict": false,
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"hasCredentials": {
"type": "hasMany",
"model": "PersonCredential",
"foreignKey": "personId"
},
"hasIdentities": {
"type": "hasMany",
"model": "PersonIdentity",
"foreignKey": "personId"
},
"hasAccessToken": {
"type": "hasMany",
"model": "PersonAccessToken",
"foreignKey": "personId"
}
},
"acls": [
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
}
],
"methods": {}
}
PersonAccessToken.json:
{
"name": "PersonAccessToken",
"base": "AccessToken",
"strict": false,
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"belongsToPerson": {
"type": "belongsTo",
"model": "Person",
"foreignKey": "personId"
}
},
"acls": [],
"methods": {}
}
model-config.json:
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models",
"./node_modules/loopback-component-passport-c/lib/models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db",
"public": false
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"Person": {
"dataSource": "db",
"public": true,
"$promise": {},
"$resolved": true
},
"PersonIdentity": {
"dataSource": "db",
"public": true,
"$promise": {},
"$resolved": true
},
"PersonCredential": {
"dataSource": "db",
"public": true,
"$promise": {},
"$resolved": true
},
"PersonAccessToken": {
"dataSource": "db",
"public": true,
"$promise": {},
"$resolved": true
}
}
datasources.json:
{
"db": {
"name": "db",
"connector": "memory",
"file": "db.json"
}
}
and in server.js I've included
boot(app, __dirname);
app.middleware('auth', loopback.token({
model: app.models.PersonAccessToken
}));
I don't know what I've been missing or doing wrong? Why is it not storing access tokens in my custom PersonAccessToken model?

Angular Schema Form not displayed

I am using angular-schema-form to display and validate a form.
I have this code in my controller:
MyApp.controller('formCtrl', ['$scope', '$rootScope','$state', '$http',
function ($scope, $rootScope, $state, $http) {
if (ENV != "dev") {
formData = localStorage.getItem("signup.v-1.0.0.json")
$scope.signupSchema=formData.schema;
$scope.signupForm=formData.form;
$scope.signupUser=formData.model;
} else {
// load the template and cache it
$http.get("/forms/signup.v-1.0.0.json")
.then(function (response) {
console.log(response);
// template loaded from the server
$scope.signupSchema = response.data.schema;
$scope.signupForm = response.data.form;
$scope.signupUser=response.data.model;
localStorage.setItem("signup.v-1.0.0.json", response.data);
});
}
}
]);
And this in signup-v-1.0.0.json:
{
"schema":{
"type": "object",
"title": "Comment",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"email": {
"title": "Email",
"type": "string",
"pattern": "^\\S+#\\S+$",
"description": "Email will be used for evil."
},
"comment": {
"title": "Comment",
"type": "string",
"maxLength": 20,
"validationMessage": "Don't be greedy!"
}
},
"required": [
"name",
"email",
"comment"
]
},
"form":
[
"name",
"email",
{
"key": "comment",
"type": "textarea",
"placeholder": "Make a comment"
},
{
"type": "submit",
"style": "btn-info",
"title": "OK"
}
],
"model":
{
}
}
In my view:
<div class="col-md-6">
<div class="featured-box featured-box-secundary default h420">
<div class="box-content" ng-controller="formCtrl">
<h4>Inscription</h4>
<p>Inscrivez vous en 2 minutes via ce formulaire.</p>
<form sf-schema="signupSchema" sf-form="signupForm" sf-model="signupUser"></form>
</div>
</div>
</div>
I'm passing the schemaForm module to my app :
MyApp = angular.module('MyApp', ['ui.router','pascalprecht.translate','ui.bootstrap','Facebook','googleplus','mgo-angular-wizard','ui','ngMaterial','ngAria','ngAnimate','schemaForm']);
Chrome does not display code, but I have no error messages.
I found the solution.
I need to broadcast 'schemaFormRedraw' to redraw the form in the ajax load result, or after data is retrieved in local storage:
$scope.$broadcast('schemaFormRedraw');

Backbone.js - Is my model is correct - in case of multiple level datas

I am getting json data from server for make the navigation menu (has the sublinks ) - for that, i am making my model as follow. is this correct..? any one help me please?
Here is the json i am getting from server:
[
{
"label": "General",
"link": "#/general",
"subLinks": [
{
"label": "Dashboard",
"link": "#/dashboard"
},
{
"label": "My Task",
"link": "#/mytask"
},
{
"label": "My Documents",
"link": "#/mydocuments"
},
{
"label": "My Templates",
"link": "#/mytemplates"
},
{
"label": "Search",
"link": "#/search"
}
]
},
{
"label": "Repositories",
"link": "#/reposotories",
"subLinks": []
},
{
"label": "SavedSearches",
"link": "#/savedSearches",
"subLinks": []
},
{
"label": "Favourites",
"link": "#/favourites",
"subLinks": []
},
{
"label": "Reports",
"link": "#/reports",
"subLinks": []
},
{
"label": "Preferences",
"link": "#/preferences",
"subLinks": []
}
]
after i receive my json i use the parse method to manipulate the models:
define(["backbone","models/model","collection/baseCollection"], function (Backbone,model,baseCollection) {
var mainLinkModel = model.extend({
defaults:{
label:"mainLink",
link:"#",
subLinks:[
label:"mainLink",
link:"#"
]
}
})
var headerCollection = baseCollection.extend({
url:function(){
return this.path + "edms/navigationLinksTest"
},
model:model,
initialize:function(){
},
parse:function(response){
var mainNavi = [];
_.each(response.navList, function(m){
// i am making new models.. but how to handle the sublinks?
mainNavi.push(new mainLinkModel(m));
})
}
});
return new headerCollection;
})
how to handle this kind of models.. any one help me in this please..?

Resources