How to track changes to the model for the ui-scroll - javascript

Specifically for javascipt
I dont know how to do it.
I have $scope.models , that I deduce the screen.
Method get: give me array-data from server
$scope.models = {
get: function (index, count, success)
{
filterModel.Enddt = $scope.filter.enddt.toISOString();
filterModel.Startdt = $scope.filter.startdt.toISOString();
filterModel.Take = $scope.filter.nodeCount.count;
filterModel.Type = $scope.filter.chooseView.value;
filterService.getElectricityData(filterModel).then(function (data) {
var results = [];
var start = Math.max(0, index);
var end = Math.min(index + count, data.length);
for (var i = start; i < end; i++) {
results.push(data[i]);
}
success(results);
});
}
};
I think so to need create method reload() or revision(), that is deprecated.
Don't send me documentation from github, that is for coffee script.
And don't send me converter coffee to js:)
Please help me :C

Related

delete not working (couldn't delete data after save)

After press the save button
var prod = [];
for (i = 0; i < $("[name='pro_name']").length; i++) {
var temp = {};
temp["pro_name"] = $("[name=pro_name'] option:selected")[i].value;
temp["model"] = $("[name=pro_model']")[i].value;
if (!isNull($scope.ext.prod[i])) {
if (!isNull($scope.ext.prod[i].id))
temp["id"] = $scope.ext.prod[i].id;
if (!isNull($scope.ext.prod[i].delete_click))
temp["delete_click"] = $scope.ext.prod[i].delete_click;
}
prod.push(temp);
}
so i tried to debug it, and it doesn't run the temp["delete_click"] = $scope.ext.prod[i].delete_click;
Store Procedure
begin
if #delete_click = 1 begin
DELETE FROM [db].[Prod_list]
WHERE [id]=#id
end
In my opinion something is wrong with the JS code, can anyone help me out?
While debugging check the values before 'if (!isNull($scope.ext.prod[i])) {'
var click = $scope.ext.prod[i].delete_click;
var product = $scope.ext.prod[i];
var ext = $scope.ext;
var scope = $scope;
if (!isNull($scope.ext.prod[i])) {
if (!isNull($scope.ext.prod[i].id))
temp["id"] = $scope.ext.prod[i].id;
if (!isNull($scope.ext.prod[i].delete_click))
temp["delete_click"] = $scope.ext.prod[i].delete_click;
}
Maybe some of these objects is reset on 'Save'.

sync call in tableau javascript

I am trying to do sync call using tableau.
var checkLen = $s_filter_i.length;
for (i = 0; i < checkLen; i++) {
var filterValfscr = $s_filter_i[i].VALUE;
Send_Tablo(filterValfscr);
}
When call this function Send_Tablo(filterValfscr) it runs the below code.
But the problem is before I get response form getSummaryDataAsync() it comes out of the function recalls Send_Tablo(filterValfscr) again I get the latest request data instead of first data request.
function Send_Tablo() {
var filter_indx = JSON.parse(window.localStorage.getItem('filter_indx'));
var arry = [];
for (var i = 0; i < filter_indx.s_filter_i.length; i++) {
Attr_lab_name = filter_indx.s_filter_i[i].ATTRIBUTELABEL;
arry.push(filter_indx.s_filter_i[i].VALUE);
}
currentViz.getWorkbook().changeParameterValueAsync('Attribute_Label', filterValfscr, Attr_lab_name, arry);
alert(filterValfscr);
var fScr_data;
sheet = currentViz.getWorkbook().getActiveSheet();
sheet.getSummaryDataAsync(options).then(function (t) {
data = t.getData();
console.log("Data", data);
frscr_demo();
});
function frscr_demo() {
//var fScr_data;
currentViz.getWorkbook().getActiveSheet().applyFilterAsync(Attr_lab_name, arry, tableau.FilterUpdateType.REPLACE);
sheet = currentViz.getWorkbook().getActiveSheet();
sheet.getSummaryDataAsync(options).then(function (t) {
fScr_data = t.getData();
console.log("FSCR", fScr_data);
var aa = $(fScr_data).length;
});
}
}
What I am try to achieve is Send_Tablo() should run all the Async function first before running the second iteration of Send_Tablo() from the for loop.
Do let me known what I am doing wrong? Thanks in advance.

YDN-DB with multiple deferred

Im trying to use multiple deferred with jquery $.when but so far no luck, this is my code:
var req = $.when(db.count('items'),db.values('items'),db.get('config', 1));
req.done(function(count,r,config) {
var currency = config.currency;
if(count > 0){
var n = r.length;
for (var i = 0; i < n; i++) {
var id = r[i].id;
var itemId = r[i].itemId;
console.log('ID: '+id+' itemID: '+itemId+' Currency: '+currency);
}
}
});
My sample isn't working so hope you guys can help me, I searched everywhere for a solution. Thanks
I see. I will see how I could implement jquery deferred list. Although ydn-db promise has done, fail and them, etc, it is not $.Deferred instance. An adaptor approach is require.
Currently, use transaction as follow:
var results = {};
var tx_req = db.run(function(tx_db) {
tx_db.count('items').done(function(x) {
results.count = x;
});
tx_db.values('items').done(function(x) {
results.values = x;
});
tx_db.get('config', 1).done(function(x) {
results.config = x;
});
}, ['items', 'config'], 'readonly');
req.done(function() {
var count = results.count;
var r = results.values;
var config = results.config;
var currency = config.currency;
if(count > 0){
var n = r.length;
for (var i = 0; i < n; i++) {
var id = r[i].id;
var itemId = r[i].itemId;
console.log('ID: '+id+' itemID: '+itemId+' Currency: '+currency);
}
}
results = null;
});
It is a bit messy, but more efficient because all three query run in a single transaction.
EDIT:
Just need to add promise() method that return an object having done, fail and progress functions. Should be doable without much overhead. Basically you can do an adaptor like:
var wrap = function(req) {
req.promise = function() {
return req; // Note: req has done, fail and progress functions.
// however, jquery api demand promise to return a new deferred.
}
return req;
}
$.when(wrap(db.count('items')),wrap(db.values('items')),wrap(db.get('config', 1)));
Here is complete code in jsfiddle.
EDIT:
From release 0.8.1, promise method is added to request object and wrapping is no longer needed. Example.

JSON.stringify comes back empty?

I'm trying to json serialize an array as follows:
function postToDrupal(contacts, source, owner) {
(function ($) {
var contact, name, email, entry;
var emails = [];
var post_object = {};
for (var i = 0; i < contacts.length; i++) {
contact = contacts[i];
emails[i] = {};
emails[i]['name'] = contact.fullName();
emails[i]['email'] = contact.selectedEmail();
console.log(contacts.length)
}
post_object['emails']=emails;
post_object['source']=source;
post_object['owner']=owner;
$.post("/cloudsponge-post",JSON.stringify(post_object),function(data) {
window.location.href = "/after-import";
});
}(jQuery));
}
The problem is, the post comes back empty. Without JSON.stringify() I get all the elements (but there are thousands of them, which can hit some servers limits, so they need to be serialized). Any help would be much appreciated.
The problem was this. When the request to the server is of type JSON, it's not strictly a POST, so PHP does not populate the $_POST field. In order to retrieve the data, it must be read directly from the request, in other words, instead of using $_POST, use:
$data=file_get_contents("php://input");
You don't need to call JSON.stringify, $.post accepts an object, check $.post.
Code to post just a few emails at a time :
function postToDrupal(contacts, source, owner) {
var pending = 0, limit = 10;
var post_patch = function(emails) {
var post_object = {};
post_object['emails']=emails;
post_object['source']=source;
post_object['owner']=owner;
pending++;
$.post("/cloudsponge-post", post_object,function(data) {
if(pending-- == 0) {
window.location.href = "/after-import";
}
});
}
(function ($) {
var contact, emails = [];
for (var i = 0; i < contacts.length; i++) {
contact = contacts[i];
emails[i] = {};
emails[i]['name'] = contact.fullName();
emails[i]['email'] = contact.selectedEmail();
console.log(contacts.length)
if(limit-- == 0) {
limit = 10
post_patch(emails);
contact = null; emails = {};
}
}
}(jQuery));
}

Javascript array results returning undefined

I have been working on a simple math game and am having problems getting the overall answer results to return after the end of the game.
Here is what my return function looks like
function pShowResults() {
var pNumResults = document.getElementById("results");
for (var i = 0; i <= 10; i++) {
pNumStore.push(pNumGuess[i]);
var pNumTable = document.createElement("div");
pNumTable.innerHTML = (pNumGuess[i]);
pNumResults.appendChild(pNumTable);
}
}
Here is the full script
Pretty much need debugging help. I new to this so I'm guessing there is a ton that's off, but as long as I can get the results fed back I should be fine.
You are not passing the value of x in many placess
$(document).ready(function () {
//declare arrays and variables for use below
var pNum1 = [];
var pNum2 = [];
var pNumAns = [];
var pNumGuess = [];
var pNumStore = [];
var pNumCarry = 0;
var pNumTrack = 0;
var pNumMessageRight = ['Awesome Job!', 'Correct!', 'Great Job!'];
var pNumMessageWrong = ['Oh No! That Was Wrong!', 'Incorrect!', 'That\'s Wrong'];
$(".Play").click(function () {
$("#popup").attr("class", "on");
pNumTrack = 0;
pNumGen(pNumTrack);
});
$(".pNumSubmit").click(function () {
pNumCalc(pNumTrack-1);
});
$(".pNumNext").click(function () {
pNumGen(pNumTrack);
});
function pNumGen(x) {
pNum1[x] = (Math.round(Math.random() * 51));
pNum2[x] = (Math.round(Math.random() * 51));
pNumAns[x] = pNum1[x] + pNum2[x];
$(".pNum1").html(pNum1[x]);
$(".pNum2").html(pNum2[x]);
$(".pNumGuess").val("");
$(".pNum1").html(pNumTrack[x]);
if (pNumTrack == 2) {
$(".pNumNext").html("");
$(".pNumSubmit").html("Close");
pShowResults();
}
pNumTrack++;
}
function pNumCalc(x) {
pNumGuess[x] = $(".pNumGuess").val();
if (pNumGuess[x] == pNumAns[x]) {
$(".message").html(pNumMessageRight[Math.floor(Math.random() * pNumMessageRight.length)]);
$(".pNumNext").html("Next Question >")
} else {
$(".message").html(pNumMessageWrong[Math.floor(Math.random() * pNumMessageWrong.length)]);
$(".pNumNext").html("Maybe The Next Question >")
}
}
function pShowResults() {
var pNumResults = document.getElementById("results");
for (var i = 0; i < pNumGuess.length; i++) {
pNumStore.push(pNumGuess[i]);
var pNumTable = document.createElement("div");
pNumTable.innerHTML = (pNumGuess[i]);
pNumResults.appendChild(pNumTable);
}
}
});
Demo: Fiddle
There is a function called pNumCalc in your code which you have set to take in an argument, but you never pass one in. You use the argument to store the results in the pNumGuess array, but since the argument is never passed in, the guesses are never stored, and you end up with undefined as the answers the user gave.
Updated fiddle: http://jsfiddle.net/dwdX9/2/. Not sure how close this is to what you actually want though, but hopefully it gets you on the right track.
Because StackOverflow wants code to to be included when JSFiddle is...:
pNumCalc(pNumTrack)
You forget to define array before use it.
function pShowResults() {
var pNumStore = new Array();
var pNumResults = document.getElementById("results");
for (var i = 0; i <= 10; i++) {
pNumStore.push(pNumGuess[i]);
var pNumTable = document.createElement("div");
pNumTable.innerHTML = (pNumGuess[i]);
pNumResults.appendChild(pNumTable);
}
}
I must suggest you should use jquery instead.
After visiting your Fiddle seems like there are many problems with the code. and also your question is unclear.
for e.g.
$(".pNumSubmit").click(function () {
//why x value not passed?
pNumCalc();
});
function pNumCalc(x) {
pNumGuess[x] = $(".pNumGuess").val();
if (pNumGuess[x] == pNumAns[x]) {
$(".message").html(pNumMessageRight[Math.floor(Math.random() * pNumMessageRight.length)]);
$(".pNumNext").html("Next Question >")
} else {
$(".message").html(pNumMessageWrong[Math.floor(Math.random() * pNumMessageWrong.length)]);
$(".pNumNext").html("Maybe The Next Question >")
}
}
Please clear which array is returning undefined so that others can help you.

Categories