pagerjs access url parameters in withOnShow - pagerjs

I'd like to (asynchronously) fetch model-data each time a page is shown. The path may have additional parameters telling the withOnShow-handler what fields to fetch, for example:
..#!/navigation/href_with_params/users/123?fields=a,b,c,d
How would you access the "fields"-param inside the withOnShow handler?
Cool.loadUser = function(callback, page) {
$.get('users/' + page.currentId + "?fields=" + <fields>, function(data) {
callback(ko.mapping.fromJSON(data));
});
};

you could do something like
var fields = page.ctx.field();
inside your withOnShow. Hope this helps.

Related

SharePoint CAML + REST + Paging issue

I suppose I have found another SP bug... but maybe I do something wrong.
I have this POST request:
https://dmsdev/coll/f7c592adcb4c4e5996c2de00d444a94c/_api/Web/Lists/GetByTitle('1')/GetItems?$expand=ContentType&$select=Id,SonarDocId,ContentTypeId,EncodedAbsURL,Modified,ContentType/Name
With body:
{"query":{"ViewXml":"<View><Query><OrderBy><FieldRef Name='Modified'/></OrderBy></Query><RowLimit>50</RowLimit></View>","ListItemCollectionPosition":{"PagingInfo":"Paged=TRUE&p_Modified=2017-08-10T07:25:28"}}}
As you can see I do a CAML query with ORDER BY Modified column and I want to take items starting from the item after the item with some modified date but looks like this is not working... I mean similar request on other SP environment works, and on the other env it is not working... it takes all items starting from the first one after ordering by modified... I have no idea what is wrong :/
You could check my sample test script.
<script type="text/javascript">
function restCallwithCaml(listName, caml,PID) {
/// get the site url
var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
/// set request data
var data = {
"query": {
"__metadata":
{ "type": "SP.CamlQuery" },
"ViewXml": caml,
"ListItemCollectionPosition": {
"PagingInfo": "Paged=TRUE&p_ID=" + PID
}
}
};
/// make an ajax call
return $.ajax({
url: siteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/GetItems",
method: "POST",
data: JSON.stringify(data),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
'content-type': 'application/json;odata=verbose',
'accept': 'application/json;odata=verbose'
}
});
}
function GetItemsPaging() {
var pageLimit = 2;
var pageNumber = 0;
var caml = "<View><Query><Where><Geq><FieldRef Name='ID'/><Value Type='Number'>1</Value></Geq></Where></Query><RowLimit>" + pageLimit + "</RowLimit></View>";
var listName = "ChildB";
restCallwithCaml(listName, caml, pageNumber).done(function (data) {
if (data.d.results.length == pageLimit) {
pageNumber++;
//add to array or display
var PID=data.d.results[data.d.results.length - 1].Id;
alert(PID);
restCallwithCaml(listName, caml, PID).done(function (data) {
//add to array or display
alert(data.d.results[data.d.results.length - 1].Id);
})
}
});
}
</script>
Original thread
The problem was with my understanding of how this whole thing works + time zones
I had to write a paging query eg:
Paged=TRUE&p_ID=10&p_Modified=2018-12-14T18:52:00
So I had to add p_Modified parameter from the last item from the previous page... Additionally this data has to be in UTC, so for example I can execute get query with the time returned by the CAML
https://server/site/_api/web/RegionalSettings/TimeZone/localTimeToUTC(#date)?#date='2018-12-14T11:52:00'
And date returned by this call should be passed in p_Modified.

Logging in to firedrive using casperjs

I'm trying to log in to Firedrive using casperjs 1.1.0-beta3 with phantomjs 1.9.0. The form does not have an id so I use findone to find the form elements.
Here is the code which runs without throwing any exceptions but doesn't log in as can be seen by capturing the output (document html) and searching for the word 'anonymous'.
Any ideas would be welcome.
Run like this:
casperjs firedrive1.js http://www.firedrive.com/myfiles username password
firedrive1.js:
// Expects url, user, and password on command line.
/*jshint strict:false*/
/*global CasperError, console, phantom, require*/
var casper = require("casper").create();
var dump = require("utils").dump;
var url = casper.cli.args[0]
var user = casper.cli.args[1]
var pass = casper.cli.args[2]
// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
casper.start(url, function() {
console.log("page loaded");
this.evaluate(function(user, pass) {
console.log("user: " + user);
console.log("pass: " + pass);
__utils__.findOne('#username').value = user;
__utils__.findOne('#password').value = pass;
console.log("user set to: " + __utils__.findOne('#username').value);
console.log("pass set to: " + __utils__.findOne('#password').value);
__utils__.findOne('#header_login_btn').click();
}, {user: user,
pass: pass})
});
casper.thenEvaluate(function(){
console.log("Page Title " + document.title);
console.log('doc: ' + document.documentElement.innerHTML);
});
casper.run();
The trick seems to be to separate the retrieval of the page using casper.start from the filling in of the form by putting that in a separate casper.then.
Here is an outline of what finally worked. Notice that the class of the form is different from the one I thought it was, this is because the page is actually a different page from the one I captured by hand from Firefox:
casper.start(url, function(){
// do nothing or log something
});
casper.then(function() {
this.fill('form.form-horizontal', {
'user': user,
'pass': pass
}, true);
});
casper.thenEvaluate(function(){
// check that the user name appears in the text of a link
});
It is possible that the issue is the click() which is the DOM click and likely does not work in PhantomJS. You should use the casper.click for this after the evaluate call:
this.evaluate(function(user, pass) {...}, {...})
this.click('#header_login_btn');
If this does not solve the problem you may try it with the various casper.fill functions. They work on forms and include the optional submit argument which you would set to true.
this.fillSelectors('form.il_login_form', {
'#username': user,
'#password': pass
}, true);
It looks like the selector for the login form on the firedrive page is form.il_login_form.
It is also possible that you need to include a casper.wait or casper.waitForSelector after login, if it is only an AJAX driven login.
If this still does not work, you may need to update PhantomJS to newest version (currently 1.9.7-15).

Make ember-data use the URL that ember is loading for the REST request

On the system I am working on right now, I have had to try to tame ember-data a bit about how it does its REST request. The way that ember-data by default figures the URL for a certain request for a model is just not gonna cut it with the backend I am using.
What I need is, to get ember-data to use the same URL that ember is loading, but with a '?json' suffix. That is, if ember switches page to my band page, and the url is /bands, I want ember-data to request /bands?json for the data it needs, not whatever it figures from the name of the model. One could say, that I wanted the URL to be calculated from the path of the loading route, instead of from the name of the model being used.
I have tried by subclassing DS.RESTAdapter{} and see if I could get the buildURL method to do this, but I can't figure out how to get the URL ember is gonna load. The buildURL method is called before ember changes the location, so I can't use document.location.href or something. I can imagine I will need a way to ask ember what it is now loading, and what the URL is.
Any ideas of how to do this?
UPDATE
There hasn't been any satisfying solutions, so I decided to just do it the dirty way. This is it:
App.RouterSignature = [
['index', '/', '/index_models'],
['bands', '/bands', '/band_models'],
['band', '/band/:band_slug', '/band_model']
];
App.Router.map(function() {
for (var i = 0; i < App.RouterSignature.length; i++) {
var route = App.RouterSignature[i];
this.resource(route[0], {path: route[1]});
}
});
App.CustomAdapter = DS.RESTAdapter.extend({
buildURL: function(record, suffix) {
var url,
suffix = '?json',
needle = this._super(record);
for (var i = 0; i < App.RouterSignature.length && !url; i++) {
var route = App.RouterSignature[i];
if (route[2] == needle)
url = route[1];
}
return url + suffix;
}
});
Now App.Routes and DS.RESTAdapter.buildURL are based off the same data. The first two values in the App.RouterSignature list is just the name of the route, the path of the route. The third value is what DS.RESTAdapter.buildURL by default guesses should be the url. My custom adapter then takes that guess, matches it with one of the items in the App.RouterSignature list and then takes the second value from that item - the routes path.
Now the requests that ember-data makes is to the same url as the routes path.
You can try to setup your Adapter like so:
App.Adapter = DS.RESTAdapter.extend({
...
buildURL: function(record, suffix){
return this._super(record, suffix) + "?json";
}
});
App.Store = DS.Store.extend({
...
adapter: App.Adapter.create();
...
});
See here for more info on the RESTAdapter buildURL method.
Hope it helps

Whats the angular way of autodirecting incomplete url's?

when i want to get to
http://www.koran-auf-deutsch.de/koran-deutsch/23-die-glaubigen-al-mominun/
and just enter
http://www.koran-auf-deutsch.de/koran-deutsch/23
i get directly to the url. i would like to get a similar behaviour
in my angular app, where would you inject that functionality? any ideas?
angular.module('app', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/:id', {controller: RedirectCtrl}).
when('/:id/:title', {templateUrl: 'partials/post.html', controller: PostCtrl}).
otherwise({redirectTo: '/404'});
}]);
function RedirectCtrl($routeParam, $http) {
var post_id = $routeParams.id;
// get title by id
$http.get('/api_url_to_get_title_by_id').success(function (data) {
window.location = "/" + post_id + "/" + data.title;
});
}
RedirectCtrl.$inject = ['$routeParams', '$http'];

titanium - get the facebook user name?

Using titanium, does anybody have some simple instructions to get the user's facebook name, once signed into facebook?
you don't need to do any of this, the username is provided in the data response after the login is done.
Look at the appcelerator documentation
I haven't tested the code but you can try this:
var fbuid = Titanium.Facebook.uid; //this would be the logged user's facebook uid
function fQuery() //this function exec the fql query
{
var myQuery = "SELECT name FROM user WHERE uid = "+fbuid;
var data = [];
Titanium.Facebook.request('fql.query', {query: myQuery}, function(x)
{
var results = JSON.parse(x.result);
var username = results[0].name; //user's fb name
});
};
Ah, here is how you do it:
function getFacebookInfo(){
Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', function(e){
if (e.success){
var jsonObject = JSON.parse(e.result);
//do something here with these values. They cannot be passed out of this
//function call... this is an asynchronous call
//that is, do this:
saveToDb(jsonObject.first_name);
} else {
//some sort of error message here i guess
}
});
};
Finally, along with name and username, check out the facebook page for the other variables you can get -
http://developers.facebook.com/docs/reference/api/
FINALLY: be aware that this is a callback, and titanium won't actually wait for this call to finish. That is, any variable declared to hold the results the returned after the requestWithGraphPAth will immediately return, and as a result almost always be empty.
I guess you could make a nifty loop that just... loops until some variable is set to false. And you'd set the variable to false in the callback... but that seems dodgy.
Just make your call back do everything else, that is, save to the db etc etc
If you do go the route of calling Ti.Facebook.authorise() to log in the user, remember to define
Ti.Facebook.addEventListener('login',function(e){
if (e.success){
...
...
} else if (e.error){ } else if (e.cancel) { }
}
before the call. And then, in the success bit, you can make a requestWithGraphPath call and so on. I just save all the details to the database and retrieve them each time after that, works fine for me!