Angular2-dart in non SPA - angular-dart

I would like to use angular2-dart in non-spa as in
loading two components in angular2 non SPA
i.e. in any non-spa page (not just index.html), I would like to have something like
<body>
<my-app-a>
loading...
</my-app-a>
Static content
<my-app-b>
loading ..
</my-app-b>
</body>
How can this be done using angular2-dart? This will allow to bootstrap arbitrary Angular2-dart component(s) in any non-spa page.

Just call bootstrap() twice in web/main.dart, like this:
void main() {
bootstrap(AppComponent);
bootstrap(App2Component);
}
I just tried it and it works fine.

Related

Prestashop module development - why is this template redirecting not working

On user-registration confirmation I want to show a simple popup. For the moment, in order to simplify I'm happy to show an "Hello World".
This is the template file, views/templates/hook/registrationConfirm.tpl
<div id="idname" class="block">
<h1 class="title_block">HelloWorld</h1>
</div>
In my custom module I have this hook (which I know is being triggered doing debug):
public function hookActionCustomerAccountAdd($params) {
return $this->display(__FILE__, 'registrationConfirm.tpl');
}
It doesn't show anything (I also tried inspect the source code of the rendered page, but I dind't find the "HelloWorld")
Hooks starting by "Action" react to an action but do not display anything, but those starting with "Display" do.
You should also react to the hook displayCustomerAccount
public function hookActionCustomerAccountAdd() {
$this->is_new_account = true;
}
public function hookDisplayCustomerAccount()
{
if ($this->is_new_account) {
return $this->display(__FILE__, 'registrationConfirm.tpl');
}
}
I tried the solution posted by #shagshag but for some reason it doesn't work for me. So I share my solution (it's not pretty, nor efficient I think, but it seem to work for me): in the hookActionCustomerAccountAdd I save on a custom table (newCustomersTmp) email and customer id, because these are the data I need after, in the display Hook. Then in the hookDisplayCustomerAccount I check if an user with the current email ($this->context->customer->email) already exists in my table: if so I retrieve the data, do the actions I need with them and delete the row in the table.

What is the proper way to integrate dynamic content into the layout.ejs file in a Sails.JS application?

Say I wrote a blog app in Sails.js.
On every page in this app, there is a sidebar widget called "Recent Posts", where it lists the titles of the 5 most recent posts and clicking on them takes you to the post in question.
Because this sidebar widget is present on every page, it should be in layout.ejs. But, here we have a conflict - dynamic content is only supposed to be pulled from the database in the controller action for rendering a specific view.
This dynamic content isn't for a specific view, it's for the whole site (via layout.ejs).
By the conventions that I understand, I'd have to get that dynamic content data for the sidebar widget in every controller action that renders a view (otherwise I would get an undefined error when I attempt to call that local in my layout.ejs file).
Things I've tried / considered:
Load that dynamic content in every controller action that renders a view (this solution is very bad) and calling that dynamic content in layout.ejs as if it were a local for the specific view. This works fine, but goes against D.R.Y. principles and quite frankly is a pain in the ass to have to run the same query to the database in every controller action.
As per another similar stackoverflow question, create a new config (E.G. config/globals.js), load my dynamic content from my database into that config file as a variable, and then calling sails.config.globals.[variable_name] in my layout.ejs file. This also worked, since apparently config variables are available everywhere in the application -- but it 's a hacky solution that I'm not a fan of (the content I'm loading is simply the titles and slugs of 5 recent posts, not a "global config option", as the solution implies).
Run the query to get the dynamic content inside the .EJS file directly between some <% %> tags. I'm not sure if this would work, but even if it did, it goes against the separation of concerns MVC principle and I'd like to avoid doing this if at all possible (if it even works).
As per a lengthy IRC discussion # http://webchat.freenode.net/?channels=sailsjs, it was suggested to create a policy and map that policy to all my controllers. In that policy, query the database for the 5 most recent posts, and set them to the req.recentposts. The problem with this solution is that, while the recent posts data will be passed to every controller, I still have to pass that req.recentposts data to my view -- making it so I still have to modify every single res.view({}) in every action. I don't have to have the database query in every action, which is good, but I still have to add a line of code to every action that renders a view... this isn't D.R.Y. and I'm looking for a better solution.
So, what is the proper solution, without needing to load that dynamic content in every controller action (a solution that adheres to D.R.Y. is what I'm lookng for), to get some dynamic content available to my layout.ejs file?
In folder /config you should create a file express.js and add something like that:
module.exports.express = {
customMiddleware: function(app){
app.use(function(req, res, next){
// or whatever query you need
Posts.find().limit(5).exec(function(err, posts){
res.locals.recentPosts = posts;
// remember about next()
next();
});
});
}
}
Then just make some simple loop in your view:
<% for(var i=0; i<recentPosts.length; i++) { %>
<% recentPosts[i].title %>
<% } %>
Here are some links to proper places in documentation:
https://github.com/balderdashy/sails-docs/blob/0.9/reference/Configuration.md#express
and
https://github.com/balderdashy/sails-docs/blob/0.9/reference/Response.md#reslocals
I found out another way to do this. What I did was to create a service that could render .ejs files to plain html by simply taking advantage of the ejs library already in sails. This service could either be invoked by the controller, or even passed as a function in the locals, and executed from within the .ejs. The service called TopNavBarService would look like:
var ejs = require('ejs');
exports.render = function() {
/* database finds goes here */
var userInfo = {
'username' : 'Kallehopp',
'real_name' : 'Kalle Hoppson'
};
var html = null;
ejs.renderFile('./views/topNavBar.ejs', {'locals':userInfo}, function(err, result) { html = result; });
return html;
}
In the constroller it could look like:
module.exports = {
testAction: function (req, res) {
return res.view('testView', {
renderNavbar: TopNavBarService.render // service function as a local!
});
}
};
This way you can create your customized ejs-helper that could even take arguments (although not shown here). When invoked, the helper could access the database and render a part of the html.
<div>
<%- renderNavbar() %>
</div>

How to detect the browsers language in the EntryPoint SmartGWT

I'm developping a project with SmartGWT and I want to make it international,but how to know the browser's language in my EntryPoint (onModuleLoad) ?
I'm using Spring in the Server Side, and in my onModuleLoad I'm sending an RPC call to my Service which is gathering data from properties Files and respond with a Map contains all my keys value for internationalization, so while creating IU Widgets I'm using my Map like this lables.get("myLabel").
now that everytings is working fine, I want to detect the browser's language and use it for querying the right properties.
Sorry if my english is so bad
com.google.gwt.i18n.client.LocaleInfo#getCurrentLocale()
then I ended with a dirty solution, I created a javascript function as Lt_Shade told me,which put the navigator.language, then I retreive it in my EntryPoint thanks to the Document object, the probleme was when I run with IE or FireFox, the EntryPoint run before my HTML Page, so before my Javascript function and I dont find my navigator.language, so what I did is to create a JSNI function which call my javascript object within my EntryPoint (Calling Javascript function from java:gwt code) so I emphasize and insist the call then I'l sure that my navigator.language is loaded and I can retreive it.
in my HTML page
....
<script language="javascript">
function loadLocaleLanguage(){
document.getElementById("localeLanguage").setAttribute("value",(navigator.language).substring(0,2); // I don't need Country code
}
</script>
...
<input type="hidden" id="localeLanguage" value="" />
....
in my EntryPoint
public static native void getLocaleLanguage() /*-{
$wnd.loadLocaleLanguage();
}-*/;
public void onModuleLoad(){
// I call my javascript function to ensure that the input have my localeLanguage
getLocaleLanguage();
// now I'm retreiving it using DOM thanks to Document object
String localeLanguage = Document.get().getElementById("localeLanguage").getAttribute("value");
....
}
I'm sure that's not the best solution but at least it works
if someone can help me find how to acces the httpRequest within the EntryPoint (it containts the accept-language) or to directly acces the navigator.language in the EntryPoint, it'll be better.
thanks guys
I am not sure if this helps but there is a question like this which includes some java code for getting the language in a Java servlet.
Automatically selecting country and language for user in Java Servlet
You could also try add a JavaScript function into your EntryPoint something like
var userLang = (navigator.language) ? navigator.language : navigator.userLanguage;
alert ("The language is: " + userLang);

Lazy load github gist files to display source code on the website

I have a couple of gists which I need to include in a website post to showcase the source code. Currently, I'm inlining each of the multiple gists at various places in the HTML with script tags, however, this would be a blocking call. So, is there a way to dynamically load the gists and paste it specific points in time.
I tried something like below :-
<html>
<body>
<div id="bookmarklet_1.js"></div>
<div id="bookmarklet_2.js"></div>
<div id="bookmarklet_3.js"></div>
var scriptMap = {'bookmarklet_1.js' : 'https://gist.github.com/892232.js?file=bookmarklet_1.js',
'bookmarklet_2.js' : 'https://gist.github.com/892234.js?file=bookmarklet_2.js',
'bookmarklet_3.js' : 'https://gist.github.com/892236.js?file=bookmarklet_3.js'};
var s, scr, holder;
for(s in scriptMap){
holder = document.getElementById(s);
scr= document.createElement('script');
scr.type= 'text/javascript';
scr.src= scriptMap[s];
holder.appendChild(scr);
}
</script>
</body>
</html>
The above didn't work for me, it seems that each script is doing a document.write internally to write the CSS and soure code. Has anyone tried this before or got it working ?
I started a project exactly for this purpose. Dynamically-embedded Gists
Try it now: http://urlspoiler.herokuapp.com/gists?id=992729
Use the above url as the src of a dynamically-created iframe, or add &format=html to get the Gist html snippet via ajax, then put it anywhere you want. (The gist in the above url also happens to be the documentation for how to use this project.)
I myself wanted to do exactly the same thing (with the addition of even removing the default gist style link) - ended up building a "generic" script loader that handles document.write calls :
https://github.com/kares/script.js
Here's how one can use it for embedding gists (and pasties) :
https://github.com/kares/script.js/blob/master/examples/gistsAndPasties.html
You can now get the HTML + CSS directly using JSONP.
I wrote a fuller answer in response to this question, but the key is that you can get the HTML + CSS using JSONP.
For example: https://gist.github.com/anonymous/5446989.json?callback=callback12345
callback12345({
"description": "Function to load a Gist without an iframe",
"public": true,
...
"div": <HTML code>,
"stylesheet": <URL of CSS file>
})

Div as Ajax.ActionLink

Is it possible to create Ajax.ActionLink which has instead of text, the whole DIV?
I'd like to map div on Ajax.ActionLink
I don't think that this will work using the standard MVC Ajax scripts. I believe that the MVC javascript is created to use an <a> element by default. On a different note, embedding a div tag within an <a> is not valid XHTML. What are you trying to achieve?
Using Jquery is probably the easiet way you want to go. As an example:
<div onclick="SomeAjaxFunction()">some div content</div>
function SomeAjaxFunction()
{
$.get('<%= Url.Action("SomeAction", "InSomeController") %>', function(data) {
$('.result').html(data); // assuming a partial view
alert('Load was performed.');
});
}
However, if you are dead set on using MS Ajax, to work with divs, you need to possibly look at the Sys.Mvc.MvcHelpers._asyncRequest function and do some of your own re-wrapping to make it usable. I have not tried or tested this, so use at your own risk. (Stick with the Jquery, there is far better help and support available.)