Ember.js more than one param in url - javascript

I have a route /changepassword and I need to add to it 2 params: iduser and resetcode. To look like this: /changepassword?iduser=123&resetcode=foo
I read other things related to this (1, 2) but I couldn't figure this out. I don't have any experience in ember and I was ask to do some modifications where I work. We are using ember 1.5.1
I tried this but it's throwing me this: Uncaught Error: Assertion Failed: Error: Assertion Failed: The URL '/changepassword?test1=aaasd' did not match any routes in your application
[EDIT]
Thanks to #GJK, I know that what I need is query params, no path params.
Now, my code looks like this:
Routes:
module.exports = App.Router.map ->
...
...
#resource('change_password', path: '/changepassword')
Controller:
module.exports = App.ChangePasswordController = Ember.Controller.extend
needs: ['config', 'viewport', 'application']
queryParams: ['test']
test: null
...
...
But, when I access http://localhost:3333/#/changepassword?test=foo is transitioning to: http://localhost:3333/#/changepassword
[OLD]
This are my routes:
module.exports = App.Router.map ->
#resource 'index', path: '/', ->
#resource('account_sign_up', path: '/signup')
#resource('unsubscribe', path: '/unsubscribe')
#resource('change_password', path: '/changepassword/:test1/:test2')
Any help? Thanks!

There are two possible ways to do this:
1) Use the params variable with multiple parameters:
#resource('change_password', path: '/changepassword/:iduser/:resetcode')
model : function (params) {
console.log(params.iduser);
console.log(params.resetcode);
}
2) Use the http://guides.emberjs.com/v1.10.0/routing/query-params/ (which I'm not sure is available for 1.5)..so I didn't include an example..

Related

how to redirect to error component when service give error?

Could you please tell me how to redirect to component when service give error ?In my app I am requesting a service , I want if I got any error some service it will show error component
Here is my code
https://stackblitz.com/edit/angular-ctwnid?file=src%2Fapp%2Ftest.service.ts
In my current example I am using resolver to get data.
const routes: Routes = [
{
path: 'home',
component: HelloComponent,
resolve: { data: TestResolver }
},
{
path: 'error',
component: ErrorComponent
},
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
}
];
I changed requested url so that I will get an error
correct url :https://jsonplaceholder.typicode.com/todos/1
wrong url :https://jsonplaceholder.typicode.com/todoss/1
I want that if I get an error it redirects to Errorcomponent.
#Injectable()
export class TestResolver implements Resolve<Observable<string>> {
constructor(private testService: TestService) {}
resolve(): Observable<any> {
return this.testService.getConfiguration();
}
}
any update ?
You have a couple of options. You could use a Guard to handle this, but because it's based on resolved data, it would probably be a good idea to make it an interceptor. Here's a post that should point you in the right direction re: interceptors
Using Router in services in Angular 4
You can handle this in your component.ts
Whenever the API call fails just use
this._service.function().subscribe(success => {
.....
},
error => {
this.router.navigate['error'];
});
where you call your service.

angular route path with parameters separated with &

This type of path is not working:
{ path: 'account/finalize?user=:user&token=:token', component: MyComponent }
I get route not found error when I access http://localhost:4200/account/finalize?user=devanshu&token=122323
But this is working:
{ path: 'account/finalize/:school/:token', component: MyComponent }
So, I can access http://localhost:4200/account/finalize/devanshu/122323
What is the problem here? Why is there an error in the first case?
you can use
path: 'account/finalize'
and send user and token as query Params when navigating
this.router.navigate(['/account/finalize'], { queryParams: { user: 'user', token: 'your token' } });
I think you do not need to pass these parameters:
user=:user&token=:token
You can get these parameters in the req.body
And if there is any particular component then you have to place the condition within the function.

Node.js Hapi route path

I need to match the following route on a Hapi server:
http://localhost:8080/messages/{deviceId}/?deviceType=phone
|___________________||________||_________||_______________|
1 2 3 4
Detail:
1 is my server url
2 is the path
3 is a parameter called deviceId, that is going to be different according to the requests.
4 is a query parameter that is attached by the emitter of the request. I do not control this attached query parameter, however I know it's always going to match ?deviceType=phone.
I tried to do as following, but the Hapi server doesn't even start..
server.route({
method: 'POST',
path: config.serverPath + '/messages/{deviceId}/?deviceType=phone',
handler: (request, reply) => {
}
});
Then I tried this:
server.route({
method: 'POST',
path: config.serverPath + '/messages/{deviceId*2}',
handler: (request, reply) => {
const parameters = request.params.deviceId.split('/');
const deviceId = parameters[0];
const attachedQueryParameter = parameters[1]; // should match '?deviceType=phone'
}
});
{deviceId*2} means that the route will match only if 2 parameters are provided.
I can then easily extract the parameters.
This route nearly works, expected when the parameters start with a '?' (error 404)...
... which is exactly the case I want to match (component n° 4 of the request starts with '?').
Can anyone help me with this tricky problem? Thanks
Tried again and still have a 404 error... My path is set to:
path: config.serverPath + '/messages/{deviceId}'
It works if I do a POST with this URL:
http://localhost:8080/messages/7d8a09d37d1e7b?deviceType=phone
But if I do a POST with this one:
http://localhost:8080/messages/7d8a09d37d1e7b/?deviceType=phone
It doesn't work... 404 error, saying 'Not found'.
I'm using hapi v15.0.1.
I found a temp. solution with this path:
path: config.serverPath + '/messages/{deviceId*}'
It works for both requests above, but with the second one the request.params.deviceId contains a '/' character at the very end...
You can set the path to
path: config.serverPath + '/messages/{deviceId}'
and use request.query to get the query parameters.
You can do it this way. No need to write cofig.serverPath. Also, you will have to use Joi for validation of query params.
path: '/messages/{deviceId}',
handler: storyController.getAllPaginatedStories,
config: {
validate: {
query: {
deviceType: Joi.string().required().valid(['phone']),
}
}
}

SailsJS - Create versioned API without blueprint

I'm trying to figure out how to create a versionned API for my application.
According to this issue https://github.com/balderdashy/sails/issues/322 , I have to use blueprint, but all blueprint is deactivated in my project (a wish from my boss).
I want to be able to have any URL like http://myapi.com/v1/my-custom-route , and so on for any version.
So far, the best way I've found is to duplicate all controller to something like v322AuthController.js and to map all routes like
'POST /v3.2.2/se-connecter' : 'v322AuthController.perform_signin'
But I think that's an ugly trick. I'm currently using Nginx and all my code is versionned with git
Thank you if you have any idea
Kai23
you have to disable all blueprints? Than you have to write all your routes an our own.
If you don't want to duplicate your controllers you can try this:
config/routes.js
module.exports.routes = {
'post /:apiversion/se-connecter' : {
controller: 'AuthController',
action: 'perform_signin',
skipAssets: true
}
....
}
So Sails passes all */se-connecter to the method "perform_signin" at your "AuthController". In your controller you have your api-version:
AuthController.js
module.exports = {
perform_signin: function (req, res) {
var apiversion = req.param('apiversion');
if (apiversion === "0.2.0") {
....
}
}
}
My approach:
Create subfolder for your controllers as:
/controllers/v1/UserController.js
/controllers/v2/UserController.js
In your routes.js file, add as follow:
'POST /api/v2/user': 'v2/UserController.create',
'POST /api/v1/user': 'v2/UserController.create',
And in your policies.js, you can add middlewares like this:
'v2/UserController': {
'create': ['isAuthenticated','isAuthorized']
}
My current sails' version is: 0.12.3

Nested resources and path

I'd like to nest resources in Ember, but to be able to access them with a short URL.
For example: mysite.com/admin will open the route: /routes/profiles/settings/admin
Is it possible to do something like that using Ember?
I'm currently using Ember 1.7 with Ember App Kit.
I tried the following but it doesn't work:
var Router = Ember.Router.extend();
Router.map(function () {
this.resource('profile', function () {
this.resource('profile.settings', { path: '/settings' }, function () {
this.resource('profile.settings.admin', { path: '/admin' });
);
});
Thanks.
Your code doesn't work because your inner most resource is inheriting the /profile path from the outer most resource and /settings from the middle resource. If you want it to be just plain /admin, you'd have to do something like this:
this.resource('profile', { path: '' }, function() {
this.resource('profile.settings', { path: '' }, function() {
this.resource('profile.settings.admin', { path: '/admin' });
});
});
However, this is going to get pretty hairy when you have more routes that each want top-level paths. You might find it easier to just declare a admin route at the top level, then redirect using the redirect hook in the route.

Categories

Resources