I need to access the parameters of the current url with Durandal.
Actually from the activate function, I'm able to do so:
function activate(routeData) {
var type = routeData.type;
var id = parseInt(routeData.id);
}
and retrieve the following parameters project and 1 of my url:
http://localhost:3231/#/next/project/1
But how can I do so from another function within my view model?
NB: I need also to retrieve next from the url.
var params = window.location.hash.split('/');
Define an action param in your router.map function
something like:
router.map([
{ route: '', title:'Your title', moduleId: ':action/project/:id', nav: true }
])
So you can access the value in your activate function via
routeData.action
Related
As i have created a method to call to edit page the code is shown below.
SelectStaff(_StaffEmit:any){
let StaffJson = JSON.stringify(_StaffEmit);
let _navigationExtras: NavigationExtras = {
queryParams: {
StaffJson
}
};
this._router.navigate(["StaffInfo"], _navigationExtras);
}
Now, this code redirects to StaffInfo.ts page which is successfully done. Here is the code where i receive my JSON data.
this._routeEdit.queryParams.subscribe(params => {
let StaffParsed = JSON.parse(params.StaffJson);
this.StaffModel.Id = StaffParsed.id;
this.StaffModel.FirstName = StaffParsed.firstName;
this.StaffModel.LastName = StaffParsed.lastName;
this.StaffModel.UserName = StaffParsed.username;
this.StaffModel.Email = StaffParsed.email;
this.StaffModel.Title = StaffParsed.title;
this.StaffModel.CellPhoneNo = StaffParsed.cellPhoneNo;
});
Now i have a problem as i call this JSON data it appends on the URL which i don't want to.
Here is the image below for it.
As i can see it uses GET verb but i don't want to show the data, any POST verb or NON-POST verb method to redirect with data would be appreciated.
The only way is to use skipLocationChange property could be used to protect a user from seeing URL change.
you could encode it in Base64, put in the query param and decode it again whenever you need
//encode to base64
btoa(JSON.stringify({username:"admin",firstname:"John",lastname:"Doe"}))
//eyJ1c2VybmFtZSI6ImFkbWluIiwiZmlyc3RuYW1lIjoiSm9obiIsImxhc3RuYW1lIjoiRG9lIn0
now put this string to the URL and whenever you need it just decode it
//decode to base64
JSON.parse(atob("eyJ1c2VybmFtZSI6ImFkbWluIiwiZmlyc3RuYW1lIjoiSm9obiIsImxhc3RuYW1lIjoiRG9lIn0"))
//"{"username":"admin","firstname":"John","lastname":"Doe"}"
Angular 7.2.0 and above. You can pass state in NavigationExtras object.
It can pass the state without reflecting state in url.
You can access data inside state that is pass by router in routed component and can be accessed inside constructor.
let StaffJson = JSON.stringify(_StaffEmit);
let _navigationExtras: NavigationExtras = {
state: {
StaffJson
}
};
this._router.navigate(["StaffInfo"], _navigationExtras);
}
XYZ Component to which router.navigate(['./XYZ`], _navigationExtras)
constructor(private router: Router) {
this.name = this.router.getCurrentNavigation().extras.state;
let StaffParsed = JSON.parse(params.StaffJson);
this.StaffModel.Id = StaffParsed.id;
this.StaffModel.FirstName = StaffParsed.firstName;
this.StaffModel.LastName = StaffParsed.lastName;
this.StaffModel.UserName = StaffParsed.username;
this.StaffModel.Email = StaffParsed.email;
this.StaffModel.Title = StaffParsed.title;
this.StaffModel.CellPhoneNo = StaffParsed.cellPhoneNo;
}
I would like to pass an object to a controllerA to another controllerB and display that object. To do it, I'm using ui-router with angularjs.
This is my controllerA which build the URL using $state.href():
const url = $state.href('home.stateA', {
objectA: objectA
});
window.open(url, '_blank');
Now, this my route file:
.state('home.stateA', {
url: '/stateA',
template: '<template-b></template-b>',
params: {
// object: null
objectA: null
}
})
And finnaly, I try to get my object in my controllerB like that:
// $scope.object = $stateParams.object;
$scope.object = $stateParams.objectA;
When I console.log $scope.object, I'm getting null which is the default value in my route file.
So what's going wrong ? I'm wondering if $window.open would not be the problem.
Thanks for helping me.
window.open(url, '_blank');
You are opening a new window and trying to pass an object.
Passing a String/Number
You can pass a string/number say id, as part of the URL, if your state URL is defined like '/stateUrl/:id'
and then you can access this using $stateParams.id
Sharing an object
You can use localStorage along with JSON.stringify and JSON.parse to share the object.
Set data
localStorage.setItem('object', JSON.stringify(object));
Get data
var object = JSON.parse(localStorage.getItem('object'));
I have just found the solution ->
ControllerA
const url = $state.href('stateA');
let newTab = $window.open(url);
newTab.objectA = objectA;
ControllerB:
$scope.objectA = $window.objectA
And the state is very simple :
.state('home.stateA', {
url: '/stateA',
template: '<template-b></template-b>',
})
I don't know if it's the best way to implement what I needed but at least it works. it may help someone else.
Thanks guys and have a nice day !
I suggest you use url parameter.
.state('home.stateA', {
url: '/stateA?object',
template: '<template-b></template-b>',
params: {
object: null
}
})
Do when open new tab page
const url = $state.href('home.stateA', {
objectA: JSON.stringify(objectA)
});
In controllerB
$scope.object = JSON.parse($stateParams.objectA);
Because when you are open new tab. The state param is lost
I have json file in which i pernamently rewrite data from my database ,so inside it i have several json object ( i mean i have json array inside my json File) i want to make one form for one json object for this reason i have used embedde forms i mean user task form and angular ui , here is my code:
v
ar jsonFile;
inject([ ‘$scope’, ‘$http’, ‘$location’, ‘Uri’, function($scope, $http, $location, Uri) {
camForm.on('form-loaded', function () {
$http.get(Uri.appUri("engine://engine/:engine/process-definition/key/my-process-key/startForm")).success(function(result){
var contextPath = result.contextPath + '/forms/';
var filePath = contextPath + 'data.json';
$.getJSON(filePath, function(json) {
jsonFile = json;
});
});
});
var jsonData1=JSON.stringify(jsonFile);
var rawData=JSON.parse(jsonData1);
var documentData = $scope.documentData = {
"id":rawData[i]["id"],
"organizationNameGE":rawData[i]["organizationNameGE"],
"organizationNameEN":rawData[i]["organizationNameEN"],
"cardNumber":rawData[i]["cardNumber"]
};
camForm.on('form-loaded', function() {
camForm.variableManager.createVariable({
name: 'documentData',
type: 'json',
value: documentData
});
});
but it throws exception that i have Unexpected end of input, but when i replace file data with custom data it works perfectly , what am i missing here?
how can i manage to generate one form for each json data object at a time?
Also i have tried this:
I have added TaskListener in user task process in java it looks like this
public class FormListener implements TaskListener {
public void notify(DelegateTask arg0) {
long id = (Long) arg0.getVariable("id");
String organizationNameGE=(String) arg0.getVariable("organizationNameGE");
String organizationNameEN=(String) arg0.getVariable("organizationNameEM");
String cardNumber=(String) arg0.getVariable("cardNumber");
arg0.setVariable("id",id);
arg0.setVariable("organizationNameGE",organizationNameGE);
arg0.setVariable("organizationNameEN",organizationNameEN);
arg0.setVariable("cardNumber",cardNumber);
}
}
and i also have this code inside my embeded form script:
inject(['$scope', '$http', function($scope, $http) {
var variableManager = camForm.variableManager;
// OnFormLoaded
camForm.on('form-loaded', function() {
// Fetch Variables
// - General
variableManager.fetchVariable('processInitiator'); // set by the engine
// - Process
variableManager.fetchVariable('id'); // set in an earlier form
variableManager.fetchVariable('organizationNameGE');
variableManager.fetchVariable('organizationNameEN');
variableManager.fetchVariable('cardNumber');
});
// OnVariablesFetched
camForm.on('variables-fetched', function() {
// After the variables are fetched, bind the value to the angular scope
// - General
$scope.processInitiator = variableManager.variable('processInitiator').value;
// - Process
$scope.id = variableManager.variable('id').value;
$scope.organizationNameGE= variableManager.variable('organizationNameGE').value;
$scope.organizationNameEN = variableManager.variable('organizationNameEN').value;
$scope.cardNumber=variableManager.variable('cardNumber').value;
});
but it doens;t gives me any result i mean it trows exception like this
SPIN/JACKSON-JSON-01004 Unable to find 'id'
what should i change to make my code work?
Just create a execution listner which executed at start of the user task, you can make it in the diagram. In the Listner implementation read the JSON as key value pair , set the key as camunda variable name and value as well. Now in the form give cam variable as the key that you have given in Listner implementation. You can implement the Listner in JavaScript /Java.
I'm looking for a classy strategy to retrieve the current user information with emberjs (and ember-data).
Currently, I do the following :
My API responds to /users/me with the current user.
In my adapter, when the retrieved object id isn't the one I'm hoping for, I add a real_id which is the id (the user's id in the database) and I replace the real returned id by what I'm expecting.
So when my adapter has "me" as user's id, but the server returns 1, I get the following json :
{"real_id": 1, "id": "me"}
This works. But I'm not a big fan, and I'd like to avoid as mush as possible to change the default content of the adapter.
Do you use any strategy for this ? What would you recommend ?
I would use a controller App.currentUserController for this.
App.CurrentUserController = Ember.ObjectController.extend({
content: null,
retrieveCurrentUser: function() {
var controller = this;
Ember.$.getJSON('/users/me', function(data) {
App.store.load(App.User, data);
var currentUser = App.store.find(data.id);
controller.set('content', currentUser);
});
}
});
get = Em.get
set = Em.set
App.AccountController = Em.Controller.extend
contentBinding: '_content.firstObject'
init: ->
set #, "_content", App.User.find()
I have this in my global
//custom route
routes.MapRoute(
"DownloadInstall", // Route name
"{controller}/{action}/{id}/{logonserver}", // URL with parameters
new { controller = "Software",
action = "DownloadInstall" } // Parameter defaults
);
//custom route
routes.MapRoute(
"DownloadHelp", // Route name
"{controller}/{action}/{id}/{logonserver}", // URL with parameters
new { controller = "Software",
action = "DownloadHelp" } // Parameter defaults
);
//default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Software", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
and I invoke custom routes in javascript (which works great) like this:
window.location.href = '/Software/DownloadHelp/' + #Model.ID +'\/' +
getLogonServer();
However, as soon as I moved this to an IIS7 box which has a virtual directory, my default routes were smart enough to prepend with the virtual name...however my javascript based routes aren't found because the virtual directory isn't prepended.
I would try and use the Url helper if I were you, but I realize the javascript function result will be a problem.
I'm not sure if that will work, but you could try and build up your link like this:
var server = getLogonServer();
window.location.href = '#Url.Action("DownloadHelp", "Software",
new { Model.Id, logonserver = ""})' + '/' + getLogonServer();
What definitely would work is making getLogonServer() an Html helper function instead of a javascript function, but I don't know if that's an option for you.
I resolved it by using #Url.Content helper as such:
window.location.href = '#Url.Content("~/Software/DownloadInstall/")' + #Model.ID +'\/' + getLogonServer();