I am creating a leader board and need to rotate views that contain data that the team wants to see. I am using WebAPI with Durandal.
I have a collection of strings that contain the name of each of the modules. This lives in Durandals app.js (called moduleList)
define(['./system', './viewEngine', './composition', './widget', './modalDialog', './events'],
function(system, viewEngine, composition, widget, modalDialog, Events) {
var app = {
title: 'Application',
moduleList: [
"petsSoldToday",
"conversionRatioToday"
]
.
.
.
};
Events.includeIn(app);
return app;
});
What I would like to do is call router.activate([moduleName]) on a loop so that all the modules will be displayed, one by one.
I have tried creating something simple in the shell.js activate function that basically calls
setTimeout(router.activate(current), 10000);
current being the next module in the list. However, this causes a javascript error
Uncaught SyntaxError: Unexpected identifier
I don't see anything in the stack that shows why either. Honestly, I don't know what else to try.
How can this be done? Can it be done at all?
The code in your setTimeout function is executed immediately and that value is then passed to the setTimeout method (given you an error, because activate does not return a function).
You should put it inside a closure:
setTimeout(function(){
router.activate(current)
}, 10000);
Related
I'm writing a serverless application which contains a file, for simplicity let's call it icecream.js. Now I have multiple build and deployment methods.
One simply takes this file, icecream.js, and moves it into lots of other code needed for the purpose of that build. Let's call the script which was built, containing the same code (at some point) as icecream.js, hotfudgesundae.js (Ice burried by delicious hot fudge). I can't control the hot fudge that makes up the rest of the built script. I also can't control the build process.
Build process number two executes a script (Let's call it sundaepie.js) which needs the module.exports value of icecream.js (We all know there must be some ice cream buried in there somewhere). It mustn't execute any of the code hotfudgesundae.js needs when requiring it.
Now there's a fourth script, SearchingSolutions.js, which require's the hotfudgesundae.js script.
The only thing I can control is sundaepie.js and icecream.js, so I can change the way that sundaepie requires icecream.js and what icecream.js includes.
PROBLEM: I need to write icecream.js in a way that the code it contains IS executed for hotfudgesundae.js, but not executed when required with sundaepie.js. The module.exports can stay the same.
What I tried: I can't just check if module.parent exists, because while hotfudgesundae.js doesn't require the script (icecream.js is baked into it when built), hotfudgesundae.js itself is required by SearchingSolutions.js.
I tried to make module.exports a function with one parameter, and if this parameter is given, return something, else execute the code that hotfudgebrownie.js needs. I would then call it like so require('icecream.js')(true) from sundaepie.js and require hotfudgesundae.js in the regular fashion from SearchingSolutions.js (since I can't control it), but it wouldn't execute the function when required without passing the brackets to invoke the function.
module.exports = function(isexport) {
if (isexport) {
return "For sundaepie";
} else {
console.log("Executing code in hotfudgesundae.js");
...
}
}
How can I get this to work? If you have any questions go leave a comment ;)
You could use a global variable (yes, that hurts):
// inside the module
if(global.isCold) /*...*/
// before you require it
global.isCold = true
I need to run some client-side javascript from a button in a form view in Odoo 8. This button runs a python method which returns this dictionary:
{"type": "ir.actions.client",
"tag": "my_module.do_something",}
do_something is defined in a .js file as follows:
openerp.my_module = function (instance) {
instance.web.client_actions.add("my_module.do_something", "instance.my_module.Action");
instance.my_module.Action = instance.web.Widget.extend({
init: function() {
// Do a lot of nice things here
}
});
};
Now, the javascript is loaded and executed properly, but even before launching the init function, Odoo loads a brand new, blank view, and once the javascript is over I can't get browse any other menu entry. In fact, wherever I click I get this error:
Uncaught TypeError: Cannot read property 'callbackList' of undefined
What I need instead is to run the javascript from the form view where the button belongs, without loading a new view, so both getting the javascript stuff done and leaving all callbacks and the whole environment in a good state. My gut feeling is that I shouldn't override the init funcion (or maybe the whole thing is broken, I'm quite new to Odoo client-side js) , but I couldn't find docs neither a good example to call js the way I want. Any idea to get that?
Sorry, I don't work on v8 since a lot of time and I don't remember how to add that, but this might help you: https://github.com/odoo/odoo/blob/8.0/doc/howtos/web.rst
Plus, if you search into v8 code base you can find some occurence of client actions in web module docs https://github.com/odoo/odoo/search?utf8=%E2%9C%93&q=instance.web.client_actions.add
Thanks to the pointers simahawk posted in another answer, I have been able to fix my js, which is now doing exactly what I needed. For your reference, the code is as follows:
openerp.my_module = function (instance) {
instance.web.client_actions.add("my_module.do_something", "instance.my_module.action");
instance.my_module.action = function (parent, action) {
// Do a lot of nice things here
}
};
I need to know if this is correct. I'm just beginning in app development using WinJS. I've identified the source of the problem and got rid of it but I don't know if that's the correct method.Please help!
// Optimize the load of the application and while the splash screen is
// shown, execute high priority scheduled work.
ui.disableAnimations();
var p = ui.processAll().then(function () {
//return nav.navigate(nav.location || Application.navigator.home, nav.state);
return nav.navigate(nav.location || app.local, nav.state)
}).then(function () {
return sched.requestDrain(sched.Priority.aboveNormal + 1);
}).then(function () {
ui.enableAnimations();
});
The problem is in the first .then(). The commented line was the default line, I've changed it for the app to work.I've absolutely no idea what it is.Please tell me what it means and what is changed. By the way, 'app' is WinJS.Application and Application is a WinJS namespace in navigator.js where the home property is located.
This error would suggest that navigator.js isn't being loaded by the time this code is executed. The Application namespace, which is entirely arbitrary and unrelated to WinJS.Application, is defined only in navigator.js, so if that file isn't loaded that namespace won't exist.
A WinJS namespace, by the way, is just a formalization of a module pattern in JavaScript that helps you keep the global namespace from getting cluttered. Declaring a namespace like navigator.js does it:
WinJS.Namespace.define("Application", {
PageControlNavigator: WinJS.Class.define(
just creates a single object in the global namespace called "Application" and then defines members for it. (You can change "Application" to anything you want, by the way. Nothing else in navigator.js relies on it, and navigator.js is something that comes from the app templates in Visual Studio and isn't part of WinJS itself.)
So again, my suspicion is that you don't have (or whatever the proper path is) in your default.html, the path to it isn't correct, or that perhaps it's being loaded after the other code is trying to execute. Try setting breakpoints on WinJS.Namespace.define and see if that file is loaded and the breakpoint gets hit.
I make a simple demo in my Pc which is working fine .But when I make fiddle to ask Question say
Uncaught Error: [$injector:nomod] Module 'myapp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
can you please tell why it is occur ? I am getting data n my pc.Actually my real Question how to refresh or call same webservice after some tome mean after 1 minutes.As in jquery we have setinterval function .how I will achieve in this angular ?
here is fiddle
http://jsfiddle.net/acboLcv2/1/
var app=angular.module("myapp");
app.factory('test', function($http) {
//This whole object is returned when the Service runs. It is a singleton
//and its properties can be accessed from any controller
return {
stationDashBoard: function(callback,error) {
$http.get('http://184.106.159.143:8180/FGRailApps/jservices/rest/a/departure?crsCode=VIC').success(callback).error(error);
}
}
});
function departureContrl($scope,test){
$scope.loading=true;
test.stationDashBoard(function(data){
console.log(data);
$scope.data=data.data;
$scope.loading=false;
//alert(data);
},function(error){
alert('error')
}) ;
}
Thanks
So there's a few things with your site you need to focus on:
Getting rid of the module error:
I think it's fixed, but heuristically I suggest:
Ensure that the declaration of an angular module includes the empty array as as already been mentioned. This is necessary, thusly:
angular.module("test", []);
Ensure you reference the angular app in the html. I suggest body for most applications:
CORS error
You're trying to load data from a different domain with $http.get(...). This won't work unless you do some kind of CORS hackery or you get the data from the same domain. Ie, you'd have to host this code on http://184.106.159.143:8180 (in this example).
Polling request
You're asking about fetching data from a server every n seconds. This is quite easy and the method you suggest with setTimeout() would work but would need to be integrated into the angular Digest Loop:
I suggest using $timeout because it will work with angular's rendering something like this (this is pseudocode, not tested):
var fetchFromServer(cb){
$timeout(function(){
$http.get(...).then(function(data){
//Do something with the retrieved data
cb(fetchFromServer); //Recurse
});
}, 15000);
};
fetchFromServer(fetchFromServer);
Otherwise you can use setTimeout as you normally would in javascript, but don't forget to call the $scope.$apply() method to render things in angular if you do it outside the digest loop or else it will appear as if there has been no effect.
I've had this happen in Chrome and IE...
If I click a link to navigate away from a page while requirejs is still loading or getting scripts, I get random errors. Sorry if this is vague...
For example, in require.js itself, I received an error today:
Unable to get value of the property 'normalize': object is null or undefined
In the following block:
//If current map is not normalized, wait for that
//normalized name to load instead of continuing.
if (this.map.unnormalized) {
//Normalize the ID if the plugin allows it.
if (plugin.normalize) {
name = plugin.normalize(name, function (name) {
return normalize(name, parentName, true);
}) || '';
}
//prefix and name should already be normalized, no need
//for applying map config again either.
normalizedMap = makeModuleMap(map.prefix + '!' + name,
I've received other errors in my own defined js files where they start executing the code before the dependencies are fully loaded. I get the feeling that when the browser is asked to navigate away from the current page, it stops all ajax calls and thus truncating some of the js.
Is there a way to prevent these sort of errors?
Avoid running code in the define function's scope. Start your app's logic once everything has loaded by writing your code in functions that get executed when your page is ready.
Also what do you mean by navigate? If you navigate away the page goes away, as well as its errors. Is this pushAPI style navigation with the app staying loaded, or a complete and full browser navigation?