I'm learning Sammy.js and using it to build a mini-application on top of a REST API I'm working on. I couldn't find this immediately, and I am probably missing something.
I'm using routes like:
this.get("/databases/:name", function () { ... });
I basically want to take the route path and pass it along to my REST API, since they largely match up anyways. I've inspected this within the callback, and found a property called path that contains the full path, including the filename. (i.e. /index.html#/databases/foo)
All I care about is what comes after the #, and I wonder if there is something baked in so I don't have to use this.path.split("#")[1].
You could just look at window.location.hash and strip off the leading #. That should be pretty much the same as pulling information out of this.path.
Related
I'm implementing multi-language support in my app, and I guess this is the last thing that I would need in order to be able to change between languages without reloading the whole app/page. (I already have a solution with full page reload.)
For a simple example let's say this is how my router looks:
Router.map(function() {
this.route('search', { path: t('search') });
this.route('item', { path: `${t('item')}/:id`);
});
The t function would be getting the correct translation for the given strings in the currently active language.
The structure of the route hierarchy won't change, the only things that need to be updated are the path strings. Application state should be kept, of course.
I'm wondering whether this is possible to do.
I am not %100 sure about the correctness of what I wrote but Router.map is executed and resources with the definitions given within this method is transformed to a DSL instance and that is then passed to the actual router maintained by Ember.Router itself. In order to achieve what you want I believe what we need is dynamic modification to the router even if it is just the paths you need yo modify not the whole route structure.
If you look at the following github issue, Ember.js no more supports dynamically adding routes (hence no dynamic modification to the existing ones I believe). With all that said, I believe what you want is not possible without reloading the whole app (hence losing the application state).
in AngularJS can I set up a style route like this:
http://www.mydomain.com/title-and-more-irrelevant-text-123456-category.html
where '123546' is an indentifier ?
with:
$locationProvider.html5Mode(true);
I must make this because I need keep the old URLs
If this is the entire path, then I guess it will be hard because it is too generic.
If however you can live with paths in the form /prefix/title-and-stuff-123456-foo.html (i.e. the entire URL would look like http://mysrv.com/ctx/#/prefix/title-and-stuff-123.html), read on.
You can define the route as: /prefix/*titleAndId. Notice the *. Then the entire string is accessible inside any controller as $route.current.params.titleAndId. You can parse this string (using an appropriate regular expression perhaps) and extract any information, then load the appropriate content.
The * in the route definition enables the argument to contain slashes too. If slashes are not required, you can use /prefix/:titleAndId and use it the same way.
I am using hash-based navigation in my AngularJS app rooted at /.
If a user navigates to my app like this:
http://example.com/?foo
A moment after the page loads, something (possibly Angular) is causing the address bar to look different than I expected.
What I saw:
http://example.com/?foo#/?foo
What I expected to see:
http://example.com/?foo#/
Why is this happening, and can I turn it off?
I'd wager you need to be in 'html5 mode' to not have the hash fragment... though I'm uncertain.
http://docs.angularjs.org/guide/dev_guide.services.$location
$locationProvider.html5Mode(true).hashPrefix('!');
In your app configuration, you can mess with that config param and it'd probably get rid of it.
This appears to be duplicating the hash with the path.
Check out the $location service. It has both path() and hash() methods. The second, duplicated part is the hash, the first part is the path.
Unless you are using html5 mode, all of Angular's part of the URL appears in the fragment. The problem is that Angular doesn't know about the base part of the URL (perhaps the ?foo was needed just to get Angular to load) so it won't attempt to manipulate it, it just puts its own stuff on as a fragment.
I suggest the best thing would be to check $window.location.search for a query string, and if you find one do the redirect to the URL you actually want yourself. You'll still need to do that redirect by assigning to $window.location rather than $location and it will force your angular app to reload but at least you'll end up where you want to get to.
Alternatively you could reconfigure your web server to make the appropriate rewrite, but you may not want to or be able to do that.
Or you tell your users to only use URLs they got from the app, not to try to make them up for themselves.
I am trying to understand how to internationalize a web-app developed with emberjs.
I found the ember-i18n package that I think is a good solution, but I can't understand how to use it.
The first thing you'll need to do is to create hash with all your localizations, you always pair up a identifier with the localized string.
The best practice here is to create a put all locales you have into a seperate file. (like loc-english.js)
Em.I18n.translations = {
'login.loginbutton': 'Login',
};
When your webapp is getting loaded, make sure you load your strings file. All string must be loaded before you render the first view with ember.
The actual use is quite simple you just use the 't' helper in your template
<button class="login">{{t login.loginbutton}}</button>
Which will result in Login
You can find more information at: https://github.com/zendesk/ember-i18n/blob/master/README.md
using Rails 3.1.0.rc4, I'm trying to access a route helper in a javascript file (event.js.erb in this case) and it seems like they are not loaded at that moment. When requesting the merged /assets/application.js file, I get:
throw Error("NameError: undefined local variable or method `events_path' for #<#<Class:0x00000001580010>:0x00000003191510>\n (in /<...>/app/assets/javascripts/event.js.erb)")
Any idea how to access the route helper in there?
UPDATE: Now there is a gem that does this for you: js-routes
The problem is that Sprockets is evaluating the ERB outside of the context of your Rails app, and most of the stuff you're expecting isn't there.
You can add things to your Sprockets context like so:
Rails.application.assets.context_class.class_eval do
include Rails.application.routes.url_helpers
end
That's all well and good, but getting the helpers that require an id to work is a little trickier. I'm going to use a technique called a URI Template:
var event_path = "<%= CGI.unescape event_path('{event_id}') %>";
which returns /events/{event_id} which you could render into a url using an object like { event_id: 1 }. See SugarJS's String#assign method as example implementation of this. You could also roll your own like I did.
You could move the file to views where it has access to the proper context, then pull it down to the client from a JS source tag. Referring to MyRailsApp::Application.routes.url_helpers may not be the best if you are writing an engine.