ExtJS - Getting ID of record that was just saved - javascript

I need to save some data and return the ID that is created in the SQL 2005 database. I need the ID to pass to another object before saving that so I can associate them correctly.
What is the best way to accomplish this with Ext? Is there anything built into the Framework that makes this simple?
Thanks.
function AddPromotionType() {
var currentDate = new Date();
var newTypeJsonObject = {
promotionTypeId: '0',
promotionType: Ext.getCmp('txtPromoType').getValue(),
updatedBy: userid,
updateDate: currentDate
}
// serialize our service object
var newLevelJsonData = Ext.encode(newTypeJsonObject);
// call the web service and pass over our service object
Ext.lib.Ajax.defaultPostHeader = 'application/json';
Ext.Ajax.request({
url: 'Service/AddPromoType',
method: 'POST',
params: newLevelJsonData,
success: function(response, options) {
AddTypeWindow.destroy();
AddTypeWindow.hide();
// refresh dropdown to reflect new data
Ext.getCmp('newPromoType').getStore().reload();
},
// if data fails to save, show message
failure: function(response, options) {
Ext.MessageBox.alert('Error saving new promotion type', response.responseText);
}
});
}

Assuming your server is passing back the updated data with the new id the response param of your success callback should contain it. When using the facilities built into Stores that automate Ajax calls (e.g., Ext Direct, REST API support, etc.) the id gets automatically updated on the appropriate Record for you and you can handle the store's add event to inspect the Record. However, since you're doing a manual Ajax call it's up to you to inspect your response if you need the id immediately.

Related

Loading JSON from Dojo JsonRest into object variable

According to this post Remove String from JSON, I want to fill a Dojo Selectbox with JSON Data. The Problem is that I have to change the JSON Data before I can handout the data to the dijit.form.select Box.
I get the data via JsonRest. The question if, how I can load the Json Data into a normal object variable? I tried this here but it did not work.
var processStore = new JsonRest({
target: "http://cnwin.ebusiness.local/activiti-rest/service/repository/process-definitions?startableByUser=kermit",
headers: {"Authorization": "Basic a2VybWl0Omtlcm1pdA=="},
allowNoTrailingSlash: false
});
var processes = processStore.query("",{});
I simply want to load the JSON data from the JsonRest store in a normal variable.
Thank you
The JsonRest store only accepts an array, so you're not able to retrieve the data, because your store is not able to read that data for you.
If you're only interested in reading the data (so no updating/creating/deleting has to be done), the easiest way is to retrieve that data using an AJAX request and manually put it inside a dojo/store/Memory store, for example:
require([ "dojo/request/xhr", "dojo/store/Memory" ], function(xhr) {
var url = "http://cnwin.ebusiness.local/activiti-rest/service/repository/process-definitions?startableByUser=kermit";
xhr(url, {
handleAs: json
}).then(function(data) {
if (data.data !== undefined) {
var myStore = new Memory({
data: data.data
});
// Do something with "myStore"
});
});
If you're interested in the full capabilities of the JsonRest store, you will have to extend it by yourself. If you look at the code you can see several AJAX requests, in:
get()
put()
remove()
query()
Now you can write your own store and extend those methods.

How to retrieve response data from xhrpost function and store that object in dojo/store?

I've created a login with REST and Dojo. I'm submitting my login form data with dojo xhrpost. The submit is executed via onClick function. Responses are returned from rest methods. How do I store the response object in a dojo/store such as dojo/Memory? That way I can retrieve it in any html page and delete the objects for logout.
dojo.xhrPost({
url: "http://localhost:8080/userservices/rest/rest/login",
form: dojo.byId("formNode"),
load: function(user,status) {
if(status.xhr.status == 200) {
alert(user); //---> which displays username from the response method in rest method
// What code for could be here for storing that user as an object to dojo store or memory to access several pages and delete the object?
window.location.href ="jobseekerdashboard.html";
}
}
});
dojo.xhrPost is deprecated. Have a look at dojo/request/xhr.
require(["dojo/request/xhr", "dojo/store/Memory"], function(xhr, Memory){
xhr("http://localhost:8080/userservices/rest/rest/login", {
method: "POST",
data: dojo.byId("formNode")
}).then(function(returnedData){
new Memory({
data: returnedData
});
window.location.href= "jobseekerdashboard.html";
}, function(err){
// Handle the error condition
}
});
If you change the window location though, you will lose the current enviroment. If you want to change the page location you will have to make a new ajax request once that page loads or you will have to pass the data in the session data.

How to store and use server response information?

I send a post to the server to create a new table entry in my database. Once the table entry is created I have the server respond with the id of the table entry. In the chrome developer tools I can see the response as just a singular number (i.e. if its the fifth entry in the table the server response is just 5). How do I store this information using javascript/YUI to be used later? Do I have to do something with the Y.io on: success function?
EDIT:
Y.io('/sessionsimulator/sessioncreate/', {
method: 'POST',
data: jdtoldstring,
headers: {
'Content-Type': 'application/json'
},
on: {
success: buildtable()
}
});
this is the code that posts the date/time and creates a session id. I can view the sqlite table afterwards and see that the session is created exactly how I wanted. the success function buildtable is the code that is called to generate the simulated data. within buildtable() i have a global variable that I am trying to set called sess_is
sess_id = Y.JSON.parse.responseText;
that statement lies within buildtable(), but when the table is created, the column that is filled with sess_id the variable is "undefined."
I can see in the developer tools the response to the url call /createsession is a number, I am just trying to pick that number and store it in sess_id variable.
If the response is just a number, you can access it from response.responseText in your IO success callback. It's a string, so you need to parse it as a number:
Y.io(url, {
//...
on: {
success: function (requestId, response) {
var id = parseInt(response.responseText, 10);
// do something with the id
}
}
});
It's usually a good idea to send JSON from the server and parse it in JavaScript when you want to send more information than just a number. You can read more about this in the IO User Guide, starting from the Response Object section.

In JQuery/Backbone, how can I override every AJAX request to add an additional parameter?

$.post('/js/register', data, function(){
});
$.get('/js/home', data, function(){
//render the template here
});
I use these in my Backbone app. It fetches the data from the server, then renders the template.
I want to override these on a global level so that when I make these calls, "data" has the current window.location (the url) as an attribute.
For EVERY ajax call, I want to append the current url that the user is currently on.
PS - I'm using Backbone push state - does that make a difference?
I think this answer should help you out https://stackoverflow.com/a/6998085/1846192
In your case:
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
options.data = $.param($.extend(originalOptions.data, { url: window.location.href }));
});

How to avoid values not to display with the url while using window.location.href to pass values from one page to other?

In my localhost url, am getting all the values which is being passed to the other page are getting displayed in the url.
I dont want it to display the values which are passing,
for example
http://localhost/accounting/credit/credit.php?prod=sdfsdfsd-12&prodId=6&batch=567567
am using window.location.href to pass the values to other page, i think that is the reason its getting added to the url. Is there any other way to pass the values other than window.location.href ? or is there any other way to pass.
Is it possible to make the url not to display the values ?
I just want the url to display as below
http://localhost/accounting/medismo/credit_note/credit.php
How can i do this ?
You can do this pretty simply using jQuery and an HTTP request:
$.ajax({
url: 'credit.php',
type: 'POST',
data: { prod: 'sdfsdf-12', prodID: 6 },
success: function (data, status) {
// Handle successful request
},
error: function (xhr, status, err) {
// Handle request failure
}
});
In this case, data is an object containing all the information you want to pass over to the given url. In your .php file you can access this information using:
$_POST["prod"], $_POST["prodID"], etc.
The tool you are using is called the GET-method to pass variables trough a URI! Another method you can use is the POST-method, which uses html forms (still visible in the source code).
For information about those two HTTP request methods, look here or use Google!
The next best method would be using a php session, where (when used properly) users won't be able to see the variables directly!

Categories