How can i load ICanHaz.js templates with node.js? - javascript

I read on ICanHaz.js documentation that i should load templates from a remote like this
$.getJSON('/myserver/templates.json', function (templates) {
$.each(templates, function (template) {
ich.addTemplate(template.name, template.template);
});
});
I have no idea how the json template should look like, would really appreciate an example ICanHaz.js json template.
Thanks

To save some time on debugging $.each requires two arguments to the callback function: iterator and actual object
$.getJSON('/myserver/templates.json', function (templates) {
$.each(templates, function (id, template) {
ich.addTemplate(template.name, template.template);
});
});
Of course you must remember to set promise since these templates are loaded in async mode.

Related

Trouble understanding the requireJS require() function

I'm new to Javascript and I'm having troubles understanding the following piece of code. I have read this post (https://stackoverflow.com/a/60669635/9867856) which suggests that mifosXComponents.js and mifosXStyles.js are being passed as arguments into the function function(componentsInit){...}, but isn't componentsInit a single function that returns a promise? How come two .js files are converted into a function? I'm confused.
require(['mifosXComponents.js', 'mifosXStyles.js'], function (componentsInit) {
componentsInit().then(function(){
require(['test/testInitializer'], function (testMode) {
if (!testMode) {
angular.bootstrap(document, ['MifosX_Application']);
}
});
});
});
If you require two modules (as per example) then RequireJS will:
load them async;
load their dependencies;
inject the modules to you callback, each as separate argument.
So you have a mistake in your code, your callback will receive actually two arguments, so you need to have two parameters:
// require two modules async,
// when they are loaded,
// they are injected to your callback
require(['mifosXComponents.js', 'mifosXStyles.js'], function (mifosXComponents, mifosXStyles) {
// now you can use both modules,
// seems that first one is a function which returns promise,
// but I have no idea what is the second one :)
mifosXComponents().then(function(){
require(['test/testInitializer'], function (testMode) {
if (!testMode) {
angular.bootstrap(document, ['MifosX_Application']);
}
});
});
});

how to call a service in vanilla js in a asp.net model

I am trying to call a service in js in a asp.net model.
can you please help me find the right structure,
here is my general code.
GetSomething: function () {
var something;
System.SomeService.GetSomething(new Report.DocumentsRequest(), null, function (response) {
return something = Report.GetResponse(response);???
});
return something = Report.GetResponse(response);???
//
},
tank you for the help
It's hard to tell out of context, but it looks as though you're attempting to mix C# code with JavaScript code. If that's the case, you cannot do that. Instead, you need to provide an endpoint (controller action) that accesses your service and returns the results as something like JSON. Then, you need to utilize AJAX (the XMLHttpRequest object) in your JavaScript to make a request to that endpoint.

How does AngularJS return a value from async call?

Watch this video,
https://www.youtube.com/watch?v=IRelx4-ISbs
You'll find that the code has a line wrote this:
$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});
This is a litter quirk:
the 'get' method of 'twitter' is obviously a async function, how does it return a value to $scope.twitterResult???
jsFiddle(Can't work cause the twitter API has changed):
http://jsfiddle.net/johnlindquist/qmNvq/
This code $scope.twitter.get({q:$scope.searchTerm}); return defer object. Angular view arranged so that view will be update when defer object resolve. It's just a convenience.
$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});
Your code is good for only angular binding.
But if you need to get data on run time, you need to write this below format.
If $scope.twitter is a URL,Then write it
$http.get($scope.twitter, {q:$scope.searchTerm}).success(function (response) {
$scope.twitterResult=response;
}
$http is must defined in contoller

RequireJS shim for script containing only a callback

I have an external API which provides a script file with javascript callback function in it. It assumes the function is implemented in my code.
eg a JS file with the following content:
aCallback({json:'stuff'});
I would like to wrap this in a requireJS module but am not too sure how to go about it.
I have tried the following shim:
define("my-wrapper", [], function () {
return function(data){ console.log(data); }
}
);
var require = {
shim: {
"my-wrapper": {exports: "aCallback"},
"http://api.con/service": ["my-wrapper"]
}
};
require(["http://api.con/service"], function (service) {});
but it says when it try's to load the service that aCallback is undefined. What have I got wrong? Is there a better way to wrap this kind of script?
You might want to try requiring the callback as a dependency instead of using shim so that it does exist in the same scope.
require(["http://api.con/service", "my-wrapper"], function (service, aCallback) {
});
It does depend on how the service expects to consume the callback though.

Calling a helper function with callback from a Jade template

I've just started working with Node.js so please forgive any stupidity!!! I'm trying to create a new application using Node.js. Im using the Express framework with a Postgresql database. The problem is that in my view, I wanted to to call a function. So I used a helper function that is called from my jade file. But because this function accesses the database, I tried to use callback inorder to make it work.
However, I can't seem to call a function from my jade template with the last argument as a function. The helper function worked fine when there was only one parameter being passed and it wasn't a callback function. But because the database query took a while, the the data was never displayed. But when I try to call a function with callback from my jade template, I get a syntax error.
My function call in my jade template:
#{ nameAndVersion(result.bu_entrep_id, function(error, result)) }
My helper function (It's simple because I was trying to get it to work):
exports.helpers= {
nameAndVersion: function(entid, callback) {
var x=1;
callback(null, x);
console.log(1);
}
};
My error:
500 SyntaxError: Unexpected token )
So, basically, I want to call a helper function from my jade template and have that function be a callback function.
You want to do :
Parse template
Retrieve data
Render template with data
Express templating is expected to do :
Retrieve data
Parse and render data
You should not have to execute complexe code once you've started rendering (what if you database is unavailable ?).
Jade helpers only have a formating purpose, not functionnal.
What you should do, instead of calling an helper, is giving the necessary data when calling the renderer.
app.get('anyPage', function(req, res) {
database.doSomeDataBaseQuery( /* Data base callback */ function(data, err) {
if(!err) res.render('pageTemplate', {dataBaseData:data});
}
});

Categories

Resources