I'm new to Ember.js.
I want to focus on TextField(in sample, id="text") after initialization,
but in ready function, doesn't work focus method...
<body>
<!-- library load -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.6.1.min.js"%3E%3C/script%3E'))</script>
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.min.js"></script>
<script type="text/x-handlebars">
{{view Em.TextField id="text"}} // want to focus it.
</script>
<script type="text/javascript">
var App = Em.Application.create();
App.ready = function() {
$('#text').focus(); // does'nt work.
}
</script>
</body>
The following code does work:
<script type="text/x-handlebars">
{{view App.TextField id="text"}} // want to focus it.
</script>
<script type="text/javascript">
var App = Em.Application.create();
App.TextField = Em.TextField.extend({
didInsertElement: function() {
this.$().focus();
}
});
</script>
Subclassing TextField and then spreading a custom View around your templates seemed a bit messy to me, so I wrote this little 1kb package that lets you do this more elegantly, directly in the template, without any further coding:
<body>
<!-- all the libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ember.js/1.2.0/ember.min.js"></script>
<script src="http://rawgithub.com/AndreasPizsa/ember-autofocus/master/dist/ember-autofocus.min.js"></script>
<!-- your template -->
<script type="text/x-handlebars">
Hello, world! {{ input }}
:
: more elements here
:
{{ autofocus }} {# <<<<-- Magic happens here #}
</script>
<!-- your app -->
<script>
Ember.Application.create();
</script>
</body>
You can get it from https://github.com/AndreasPizsa/ember-autofocus
or with bower install ember-autofocus. I appreciate feedback!
Related
I have written a html with a div.
I have two hbs files. I rendered one as partial and other as normal. I am not able to invoke action on the templates. I am getting missing helper error.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ember Starter Kit</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="holder">
</div>
<script src="js/libs/jquery-v1.11.1.js"></script>
<script src="js/libs/handlebars-v1.3.0.js"></script>
<script src="js/libs/ember-v1.6.1.js"></script>
<script src="js/libs/ember-data.js"></script>
<script src="js/app.js"></script>
</body>
</html>
JS:
var data={title: "My New Post", body: "This is my first post!"};
function getTemplate(templateName,hbsPath,type){
$.ajax({
url: hbsPath, //ex. js/templates/mytemplate.handlebars
cache: true
}).done(function (src) {
if (type === "partial") {
Handlebars.registerPartial(templateName, $(src).html());
} else {
template = Handlebars.compile($(src).html());
temp=template(data);
$("#holder").append(temp);
}
});
}
getTemplate("dummy","/Test/templates/dummy.hbs","partial");
getTemplate("dummy","/Test/templates/application.hbs");
App = Ember.Application.create({});
App.Router.map(function(){
this.resource('dummy');
});
App.ApplicationController=Ember.ObjectController.extend({
needs:['dummy'],
actions:{
refresh: function(){
alert("application template refresh");
}
}
});
App.DummyController=Ember.ObjectController.extend({
actions:{
refresh: function(){
alert("dummy template refresh");
}
}
});
HBS:
application.hbs:
<script id="application" type="text/x-handlebars-template">
{{> dummy}}
</script>
dummy.hbs:
<script type="text/x-handlebars" data-template-name='dummy'>
<div {{action 'refresh' target="controllers.dummy"}}>
Refresh
</div>
<div {{action 'refresh'}}>
Refresh
</div>
</script>
The code in ur demo and the code in question are different. Here is a fixed demo based on the code in ur question.
Fixing the demo u provide may need you to include the vanilla handlebars library as ember is now using HTMLBars which is built on top of handlebars but a bit different.
Em.Handlebars.compile is now actually using HTMLBars compile method. You can see here.
You may need to use something like https://github.com/rwjblue/broccoli-ember-inline-template-compiler
I'm trying to create a simple TODO app based on the one in the Getting Started Ember.js tutorials, but with persistence to a backend.
I've had everything working until I wanted to look at wrapping my markup for rendering a todo in a view (called 'todo-element'), to enable drag & drop sorting.
Then, my view refused to render. I've simplified the markup for the view just to render the text 'Hello', to try to debug things. The instance of the view sets the tagName to 'li'. So I should be getting a list of lis with the text 'Hello' in them. However, all I get is a list of empty lis.
This is the markup:
<!DOCTYPE html>
<html>
<head>
<title>Todos</title>
</head>
<body>
<script type="text/x-handlebars">
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="todos">
<section id="todos">
<header>
<h1>The TODOs</h1>
{{input type="text" placeholder="Enter new TODO description here ..."
value=newDescription action="createTodo"}}
<button {{action "createTodo"}} id="add-todo">Add</button>
</header>
<ul id="todos">
{{#each itemController="todo"}}
{{todo-element}}
{{/each}}
</ul>
</section>
</script>
<script type="text/x-handlebars" data-template-name="todo-element">
<span>Hello</span>
</script>
<script type="text/javascript" src="javascripts/jquery"></script>
<script type="text/javascript" src="javascripts/handlebars-1.0.0.js"></script>
<script type="text/javascript" src="javascripts/ember.js"></script>
<script type="text/javascript" src="javascripts/ember-data.js"></script>
<script type="text/javascript" src="javascripts/todos/app.js"></script>
<script type="text/javascript" src="javascripts/todos/todo.js"></script>
<script type="text/javascript" src="javascripts/todos/router.js"></script>
<script type="text/javascript" src="javascripts/todos/todos_controller.js"></script>
<script type="text/javascript" src="javascripts/todos/todo_controller.js"></script>
<script type="text/javascript" src="javascripts/todos/todo_element_view.js"></script>
<script type="text/javascript" src="javascripts/todos/edit_todo_view.js"></script>
</body>
</html>
And this is the code for the view:
Todos.TodoElementView = Ember.View.extend( {
templateView : 'todo-element',
tagName : 'li'
} );
Ember.Handlebars.helper( 'todo-element', Todos.TodoElementView );
My best guess is that the 'todo-element' markup is not getting associated with the TodoElementView, as I get exactly the same empty list of lis if I change the 'todo-element' markup's data-template-name to something random.
If anybody could help me with where I'm going wrong here, I'd be very grateful.
Cheers,
Doug.
think you are little confused between ember components and ember views
1. A view is called with view helper {{view App.ViewName}}. Your code {{todo-element}} looks more like a component.
In your component you have a static tag span hello so I guess it's showing only hello. Put dynamic data in it using {{}}
//index.html
<html>
<head>
<title>An Ember dice roller</title>
</head>
<body>
<script type="text/javascript" src="C:/Users/Guest/Downloads/ember/starter-kit-1.0.0/js/libs/jquery-1.9.1.js"></script>
<script type="text/javascript" src="C:/Users/Guest/Downloads/ember/starter-kit-1.0.0/js/libs/handlebars-1.0.0.js"></script>
<script type="text/javascript" src="C:/Users/Guest/Downloads/ember/starter-kit-1.0.0/js/libs/ember-1.0.0.js"></script>
<script type="text/javascript" src="C:/Users/Guest/Downloads/ember/starter-kit-1.0.0/js/app.js"></script>
<script type="text/x-handlebars" >
<h1>Dice Roller</h1>
{{#link-to 'roll'}}Click here{{/link-to}}
{{#link-to 'index'}}Go to index{{/link-to}}
{{outlet}}
</script>
<script type="text/x-handlebars" id="roll">
Hi!
</script>
<script type="text/x-handlebars" id="index">
<p>
Our content goes here
</p>
</script>
</body>
</html>
//app.js
var Roller = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_BINDINGS: true,
LOG_VIEW_LOOKUPS: true,
LOG_STACKTRACE_ON_DEPRECATION: true,
LOG_VERSION: true,
debugMode: true
});
Roller.Router.map(function () {
this.resource('index');
this.resource('roll');
});
What is wrong with this code?
I am getting the following error. My index.html page doesn't load anything.
I am beginner for ember.js.Can anyone please help me?
Assertion failed: The URL '/' did match any routes in your application
By default ember create something like this:
Roller.Router.map(function () {
this.route('index', { path: '/' });
});
But your router is overriding the index route with this:
this.resource('index');
So to be able to render the index template you will need to access your app with http://yourhost/#/index intead of http://yourhost/.
If you update your router mapping, and remove the this.resource('index') all will works:
Roller.Router.map(function () {
this.resource('roll');
});
This is a updated jsbin with this working http://jsbin.com/ucanam/1499/edit
I'm trying to setup an emberJS app, very basic. Here it is:
<!doctype html>
<html>
<head>
<script src="js/jquery.js"></script>
<script src="js/handlebars-1.0.rc.1.js"></script>
<script src="js/ember.js"></script>
<script>
window.App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend({
firstName: "Trek",
lastName: "Glowacki"
});
</script>
</head>
<body>
<script type="text/x-handlebars" data-template-name="application">
<div>
Name: {{firstName}} {{lastName}}
</div>
</script>
</body>
</html>
For some reason only Name: is rendered. Any suggestions why 'firstName' and 'lastName' are empty ?
Cheers
The problem was solved by updating the libraries used (see answer from Sean Keating)
I am using Ember and my view is getting rendered at the very bottom of my HTML DOM, instead of inside my div element.
Here is my source code :
index.html :
<body>
<div id="test">
<script type="text/x-handlebars">
{{view Skills.RecommendedSkillsListView}}
</script>
</div>
<script type="text/x-handlebars" data-template-name="recommended_skills_list">
<a href="#" {{action "b"}}>DO A NEW THING</a>
</script>
<script src="js/libs/jquery-1.8.3.min.js"></script>
<script src="js/libs/handlebars-1.0.rc.1.js"></script>
<script src="js/libs/ember-1.0.0-pre.2.js"></script>
<script src="js/app.js"></script>
</body>
app.js :
Skills = Ember.Application.create({});
Skills.RecommendedSkillsListView = Ember.View.extend({
templateName: 'recommended_skills_list',
b: function(v) {
alert('new hello');
}
});
Skills.initialize();
The rendered document :
<body class="ember-application">
<div id="test"></div>
<script src="js/libs/jquery-1.8.3.min.js"></script>
<script src="js/libs/handlebars-1.0.rc.1.js"></script>
<script src="js/libs/ember-1.0.0-pre.2.js"></script>
<script src="js/app.js"></script>
<div id="ember135" class="ember-view">
<div id="ember140" class="ember-view">
DO A NEW THING
</div>
</div>
</body>
Note: when using the same code with a previous version of ember (0.9.5) it works as expected
Ah, it looks like it is 'appending your view' to the body which you have given the 'application' template name which Ember will look for. Why not try putting a div inside the body and then referencing this in the Ember.Application.create();?
Ember.Router
Skills = Ember.Application.create({
rootElement: '#test'
});
View
<body>
<div id="test"></div>
<script src="js/libs/jquery-1.8.3.min.js"></script>
<script src="js/libs/handlebars-1.0.rc.1.js"></script>
<script src="js/libs/ember-1.0.0-pre.2.js"></script>
<script src="js/app.js"></script>
<div id="ember135" class="ember-view">
<div id="ember140" class="ember-view">
DO A NEW THING
</div>
</div>
</body>
Let me know if I've missed the point ;)
You need to specify the element to render your application to using the rootElement (docs here, related question: Emberjs rootElement) property of your application:
Skills = Ember.Application.create({
rootElement: "#test"
});
Here is a working example: jsfiddle example
Rendered output from example:
<body>
<div id="test" class="ember-application">
<div id="ember135" class="ember-view">
<div id="ember140" class="ember-view">
DO A NEW THING
</div>
</div>
</div>
</body>