need direct access to an object that can reveal information about the query parameter passed.
Would prefer a way to get hold of an object just like you could do with injector for an $http object
var $http = angular.injector(["ng"]).get("$http");
what's the most direct/short/concise/eloquent way to get the query variables without going through a controller.
Alternatively other options please.
Here is an Example to get query parameter
In app.js for example
.when('/catagory/:catagoryid/:catagoryname',
{
templateUrl:'partials/bookOfParticularCatagory.html',
controller:'bookOfParticularCatagoryController'
})
In controller
app.controller("bookOfParticularCatagoryController",function($routeParams,$scope,$http){
$scope.catagoryid= $routeParams.catagoryid;
$scope.catagoryname= $routeParams.catagoryname;
Related
I have an API in ExpressJS and a middleware that gets executed before each endpoint controller:
app.use(segregationMiddleware);
app.get('/some-endpoint', controller1);
app.get('/some-endpoint-2', controller2);
The segregationMiddleware is used to look for some parameters in the request and then it calculates a value that then is stored in the request object as req.locals.domain so the controllers can access it.
In each Mongoose model, I define a field named domain using a Mongoose plugin (so I don't have to do it every time). That field is used to segregate my assets. This means that when the segregationMiddleware populates req.locals.domain = 'foo' for example, if I make a model.find({}) I want to get only assets that have { domain: 'foo' }. Same thing if I try to update, save, delete, and so.
Of course, I can just simply modify the query on each controller since I have accesso to req, but I need to do it every time and I need to remember it for finds, findAndUpdate, save, and soo... sooner or later I'm gonna forget it.
I can define some hooks in Mongoose that will modify the query using a plugin so it adds the domain constraint to the query so I don't have to do it in the controller BUT I don't have the current req object in the Mongoose plugin unless I pass it, and the only way that come to my mind is to abstract the DB methods in the plugin, so in the controller, I do something like this:
model.safeFind(req, query);
And in the plugin I define safeFind like:
safeFind = () => {
const theRealQuery = Object.assign({}, query, { domain: req.locals.domain });
return this.find(query);
}
BUT, in this way, I need to redefine each DB query function (find, findOne, update, save...) and I need to remember to use the safe methods. Then again, I'm going to forget it sooner or later.
Is there a way I can still use the regular Mongoose methods from the controller and have the plugin somehow modify the query for every method using the current req object?
On button click I am redirecting to another page using state.go(). the code is
$scope.usercal = function(x,y,z){
$state.go('app.calendar',{employeeName:x,employeeID:y,projectName:z});
}
I want to do the same with `$location.path(/url);
But how to pass the parameters?
hi to pass Parameters for $location.path you should use $location.search() like this:
$location.path('/myURL/').search({param: 'value'});
This will lead to
/myURL/?param=value
If your parameters are the part of url (path) only than you can use
$location.path('/myURL/'+x+'/'+y);
if parameter is query string then go with
$location.path('/myURL/').search({employeeName: x});
We use ngRoute, which operates slightly differently than ui-router.
$stateParams in ui-router is converted to $routeParams, which can contain values in the same way, and is populated using url's as usual like so:
some/path/:a/to/dir/:b results in the params to the resulting page be {a, b}.
I'm using toJSON() method of my model in Sails in order to control the visibility of some of it's properties, when model is exposed via application's API.
In order to decide which properties to display and which to omit I need to know the permissions of the current user. So, how do I get the current user from inside the model? Or is there a better way (pattern) to solve this problem?
Here's some sample code I want to achieve:
toJSON: function () {
var result = {};
result.firstName = this.firstName;
result.lastName = this.lastName;
// Exposing emails only to admin users.
if (currentUser.isAdmin()) {
result.email = this.email;
}
return result;
}
Your asking about reading a session inside the model call. Currently the way sails and waterline are built you can not do this.
You can use the select property on your initial model call to restrict the columns returned. Since this would be in the context of your controller you would have access to the req object.
Here are a bunch of related questions / answers on this topic.
sails.js Use session param in model
Is it possible to access a session variable directly in a Model in SailsJS
https://github.com/balderdashy/waterline/issues/556
https://github.com/balderdashy/waterline/pull/787
Sails Google Group Discussion on the topic
I try to access the value of my json objects in sap ui5, but the getproperty function cannot access the required data.
But, at first, I have created a xsodata file with some service definitions, e.g.
"CUSTOMER_ATTR_G3" as "Customers";
Then I try to get these data in view.js file with the following code:
oModel.loadData("UserInterface_G3/SERVICES/CUSTOMER_ATTR_G3.xsodata/Customers?$select=CUSTOMER_ID,CUSTOMER_DESCRIPTION&$format=json");
When I am using console.log(oModel) I see in the odata section that the values are in the object but I cannot access to them. The following screenshot should show the structure of the object:
I tried for instance:
console.log(oModel.getProperty('/CUSTOMER_DESCRIPTION'));
or
console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));
But I cannot access the values of the object.
Does anybody have an idea on that?
console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));
You need to access your Property like that:
console.log(oModel.getProperty('d/results/0/CUSTOMER_DESCRIPTION'));
You forgot the position inside of your Array. Your Path needs the position, so if you want to get the first entry CS_0001 then you have to write result/0/CUSTOMER_DESCRIPTION.
EDIT:
Actually it depends on your Model, how you have to access the Property. Can you pls show me how you defined your oModel?
I have some route like /ads/:ad_id and from my controller I can do
this.transitionToRoute('ads.ad', adObj)
How can I do the similar thing but this time passing the ID instead of the loaded object?
O course I understand that I can load an obj by ID first, but Ember's power is in doing lost of boilerplate for us.
Update: So, as by default Ember serializes the model to URL params by doing like
mode_instance -> { model_name_id: model_instance.id }
My trivial attempt was doing
this.transitionToRoute('ads.ad', { id: adObjId })
But when passed a model object Ember does not re-fetch it.
So, the question: I have a route (single ad view) that depends on ad ID. I have this ID as number. I want to transition to this route like if I simply entered the url /ads/ID
This can be accomplish by passing the URL to transitionTo. For example,
this.transitionToRoute('/ads/' + adObjId)
The model() method will be called with the params from the URL.
Here is a use case for this:
Transitioning from a list view to a detail view. In the list view, the records don't have any relations tied to them, but the detailed view should side-load relational data. For this reason, the models are not 1:1 between the list view and detailed view. There should be a way to transition simply using the id.
Cp
What's your use case for this? Most cases when you would want to specify an object by id, you already have the object to pass to transitionTo. Can you provide more context about what you're trying to do? I think you can probably accomplish it without using the object id.
In any case, I don't think there's a good way to do this, because when you transition via transitionTo(someRoute, someModel), the route's model hook is not called, and the model you pass in (someModel) is supplied directly to the other route hooks (setupController(controller, model), redirect(model), renderTemplate(controller, model)).
See Ember.JS Route api -- model method for more details.