Ionic Worklight Page not displaying after HTTP request - ionic-framework

I'm having an issue displaying the content in the page after the Worklight http request has been executed.
The weird thing is that when I go to another page and I come back, the content gets displayed. It's like if it needs to be refreshed or something. I can see the console.log() data was received, but page was not refreshed.
This is my code:
$stateProvider.state('accounts', {
url: "/accounts",
templateUrl: 'views/accounts.html',
controller: function($scope, $ionicScrollDelegate, $rootScope){
var req = new WLResourceRequest("/adapters/JavaMQ/bankmq/getAccounts/"+$rootScope.globalReqUserId, WLResourceRequest.GET);
req.send().then(function(resp){
var x2js = new X2JS();
resp.responseText = x2js.xml_str2json(resp.responseText); //to JSON
$scope.reqUserId = resp.responseText['ASI_Message']['Riyad_Bank_Header']['Requestor_User_ID'];
$scope.accountsList = resp.responseText['ASI_Message']['Repeating_Group_Section']['Repeating_Group'];
console.log($rootScope);
})
}
});
UPDATE:
I noticed that I also keep getting the following when I moved the project to Windows (Never happened in my mac)
Deviceready has not fired after 5 seconds
Channel not fired: onCordovaInfoReady
Channel not fired: onCordovaConnectionReady

I don't really know Worklight but the documentation indicate that the send().then() handles both the onSuccess and onFailure.
Maybe the then() is expecting 2 parameters like this:
var request = WLResourceRequest(url, method, timeout);
request.send(content).then(
function(response) {
// success flow
},
function(error) {
// fail flow
}
);
If that doesn't work, can you put a breakpoint at the start of var x2js = new X2JS(); and tell us what happens?

Related

Kill the page from loading feather

I am using Ionic 2 and would like to kill the page from feather loading similar to PHP die(); function
Below is the method that I currently working with.
fetch_data() {
let loader = this.loadingCtrl.create({ content: 'Loading...' });
loader.present();
this.bank.types().subscribe( response => {
this.linkBankTypes = response.results;
loader.dismiss();
}, err => {
loader.dismiss();
loader = this.loadingCtrl.create({ content: 'No Internet connection. Make sure Wi-Fi or cellular data is turned on, then try again.' });
//Kill the page from here
});
}
PHP's die() function stops creation of the page and lets PHP run environment to return a nasty error message to the client browser.
You can cause the Ionic to crash by throwing exceptions, but that is not a pleasant user experience and not recommended.
Instead you either navigate to an error page or show a message indicating the error.
The NavController's push or popcan be used to navigate to an error page or back to the previous page.

How to wait the page to test is loaded in non angular site?

I've tried this:
browser.wait(function () {
return browser.executeScript('return document.readyState==="complete" &&' +
' jQuery !== undefined && jQuery.active==0;').then(function (text) {
return text === true;
});
}, 30000);
If jQuery.active==0 then page is completely loaded. This should work for sites with JQuery and non angular pages.
However, I have many problems of instability to test for non angular sites.
How to fix this?
By default protractor waits until the page is loaded completely. If you are facing any error then it is because protractor is waiting for the default time to be completed, that you have specified in your conf.js file to wait until page loads. Change the value to wait a for longer time if you think your app is slow -
// How long to wait for a page to load.
getPageTimeout: 10000, //Increase this time to whatever you think is better
You can also increase the defaultTimeoutInterval to make protractor wait a little longer before the test fails -
jasmineNodeOpts: {
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 30000
},
If you want to wait for any particular element, then you can do so by using wait() function. Probably waiting for last element to load is the best way to test it. Here's how -
var EC = protractor.ExpectedConditions;
var lastElement = element(LOCATOR_OF_LAST_ELEMENT);
browser.wait(EC.visibilityOf(lastElement), 10000).then(function(){ //Alternatively change the visibilityOf to presenceOf to check for the element's presence only
//Perform operation on the last element
});
Hope it helps.
I use ExpectedConditions to wait for, and verify page loads. I walk through it a bit on my site, and example code on GitHub. Here's the gist...
Base Page: (gets extended by all page objects)
// wait for & verify correct page is loaded
this.at = function() {
var that = this;
return browser.wait(function() {
// call the page's pageLoaded method
return that.pageLoaded();
}, 5000);
};
// navigate to a page
this.to = function() {
browser.get(this.url, 5000);
// wait and verify we're on the expected page
return this.at();
};
...
Page Object:
var QsHomePage = function() {
this.url = 'http://qualityshepherd.com';
// pageLoaded uses Expected Conditions `and()`, that allows us to use
// any number of functions to wait for, and test we're on a given page
this.pageLoaded = this.and(
this.hasText($('h1.site-title'), 'Quality Shepherd')
...
};
QsHomePage.prototype = basePage; // extend basePage
module.exports = new QsHomePage();
The page object may contain a url (if direct access is possible), and a pageLoaded property that returns the ExepectedCondition function that we use to prove the page is loaded (and the right page).
Usage:
describe('Quality Shepherd blog', function() {
beforeEach(function() {
// go to page
qsHomePage.to();
});
it('home link should navigate home', function() {
qsHomePage.homeLink.click();
// wait and verify we're on expected page
expect(qsHomePage.at()).toBe(true);
});
});
Calling at() calls the ExpectedCondidion (which can be be an and() or an or(), etc...).
Hope this helps...

Firefox SDK block content-type

I am developing an add-on for Firefox and i want to block a special kind of requests in content-type.
For example i want to block the application/x-rar content-types and show a message in console.log
You are able to intercept the requests by observing the http-on-examine-response notification event and check getResponseHeader('Content-Type') for application/x-rar.
'use strict';
const {
Ci, Cr
} = require('chrome');
const events = require('sdk/system/events');
events.on('http-on-examine-response', function(event) {
let channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
let contentType = channel.getResponseHeader('Content-Type');
if(contentType === 'applicaiton/x-rar'){
event.subject.cancel(Cr.NS_BINDING_ABORTED);
console.log('Aborted Request', channel.name);
}
});
Best of luck with developing your add-on.

GeoLocation in a webview inside a Chrome Packaged App

I'm trying to get geolocation inside a webview in a Chrome Packaged App, in order to run my application properly. I've tried several ways to get the permission in manifest.json and injecting scripts, but it doesn't work and doesn't show any error message.
Could someone give me a light or a solution to get permission and show my geolocation?
Some features that usually require permissions in a normal web page are also available in a webview. However, instead of the normal popup "the website xyz.com wants to know your physical location - allow / deny", the app that contains the webview needs to explicitly authorize it. Here is how it works:
No need to change the web page inside the webview;
In the app, you listen for permissionrequest events on the <webview> element:
webview.addEventListener('permissionrequest', function(e) {
if ( e.permission === 'geolocation' ) {
e.request.allow();
} else {
console.log('Denied permission '+e.permission+' requested by webview');
e.request.deny();
}
});
One thing to note is that the request doesn't need to be handled immediately. You can do whatever you need to do before allowing or denying, as long as you call preventDefault in the permissionrequest event and keep the event object from being garbage collected. This is useful if you need to do any async operation, like going to a storage to check if the URL requesting a permission should be allowed or not.
For example:
webview.addEventListener('permissionrequest', function(e) {
if ( e.permission === 'geolocation' ) {
// Calling e.preventDefault() is necessary to delay the response.
// If the default is not prevented then the default action is to
// deny the permission request.
e.preventDefault();
setTimeout(function() { decidePermission(e); }, 0);
}
});
var decidePermission = function(e) {
if (e.url == 'http://www.google.com') {
e.request.allow();
}
// Calling e.request.deny() explicitly is not absolutely necessary because
// the request object is managed by the Javascript garbage collector.
// Once collected, the request will automatically be denied.
// If you wish to deny immediately call e.request.deny();
}
Also note that your app needs to also request the respective permission:
"permissions": ["geolocation"],
The webview sample has more code for other permissions, like pointerLock and media capture.
A bit more detail:
The response does not need to be made immediately as long as you preventDefault(). The default action is to deny the permission request.
webview.addEventListener('permissionrequest', function(e) {
if ( e.permission === 'geolocation' ) {
// Calling e.preventDefault() is necessary to delay the response.
// If the default is not prevented then the default action is to
// deny the permission request.
e.preventDefault();
setTimeout(function() { decidePermission(e); }, 0);
}
});
var decidePermission = function(e) {
if (e.url == 'http://www.google.com') {
e.request.allow();
}
// Calling e.request.deny() explicitly is not absolutely necessary because
// the request object is managed by the Javascript garbage collector.
// Once collected, the request will automatically be denied.
// If you wish to deny immediately call e.request.deny();
}

Titanium xhr events not firing

I have been trying to get a simple xhr request to work but for some unknown reasons nothing happens, not even the onerror function fires off.
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function() {
Titanium.API.log('Success');
}
xhr.onerror = function() {
Titanium.API.log('Error');
}
xhr.open("GET","http://www.google.com/");
xhr.send();
I have tried this with a new created project and still no luck. Using little snitch I noticed that a connection is made by the app to the given url ... but still nothing fires off.
What am I missing?
Also I'm developing on an iPhone Simulator.
I don't think there's anything wrong with the XHR request - the Titanium.API.log function takes two arguments, but you're only giving it one, so it's probably just not printing to the console. The Titanium documentation is down at the moment so I can't link you to the correct API, but if you change your code to use Ti.API.info, for example, you should see something printed. This works for me:
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function() {
Titanium.API.info('Success');
}
xhr.onerror = function() {
Titanium.API.info('Error');
}
xhr.open("GET","http://www.google.com/");
xhr.send();