在ember js实现后网站没有呈现

时间:2014-11-18 14:24:47

标签: javascript jquery html css ember.js

背景

我正在尝试使用ember.js及其依赖项构建博客。基本上,我在左侧“导航栏”,内容显示在右侧。类似的东西:

BLOG LAYOUT

左侧不移动而右侧;另外,左侧有各种页面的链接。

问题

我正在使用ember.js来实现帖子的链接,并将帖子呈现给博客内容方面。目前,我的实现不呈现任何东西。以下是代码:

App = Ember.Application.create({});

var posts = [{
    id: '0',
    title: 'Title and title',
	  author: {name: 'Author'},
	  date: new Date('15-11-2014'),
	  content: 'Work and work.'
	},{
	  id: '1',
	  title: "Rails is Omakase",
	  author: { name: "d2h" },
	  date: new Date('12-27-2012'),
	  excerpt: "There are lots of à la carte software environments in this world. Places where in order to eat, you must first carefully look over the menu of options to order exactly what you want.",
	  body: "I want this for my ORM, I want that for my template language, and let's finish it off with this routing library. Of course, you're going to have to know what you want, and you'll rarely have your horizon expanded if you always order the same thing, but there it is. It's a very popular way of consuming software.\n\nRails is not that. Rails is omakase."
}, {
	  id: '2',
	  title: "The Parley Letter",
	  author: { name: "d2h" },
	  date: new Date('12-24-2012'),
	  excerpt: "My [appearance on the Ruby Rogues podcast](http://rubyrogues.com/056-rr-david-heinemeier-hansson/) recently came up for discussion again on the private Parley mailing list.",
	  body: "A long list of topics were raised and I took a time to ramble at large about all of them at once. Apologies for not taking the time to be more succinct, but at least each topic has a header so you can skip stuff you don't care about.\n\n### Maintainability\n\nIt's simply not true to say that I don't care about maintainability. I still work on the oldest Rails app in the world."  
}];

/*
  This will load the lists for the template to take
*/
App.ApplicationRoute = Ember.Route.extend({
  model: function(){
    return posts;
  }
});

App.Router.map(function(){
  this.resource('post', {path: '/posts/:post_id'});
});

App.PostRoute = Ember.Route.extend({
  model: function(params){
    return posts[params.post_id];
  }
});

/*Some small variables that*/
App.name = "Blog name";
App.tag = "TAG";
App.stub = "Stub";
@import url(https://fonts.googleapis.com/css?family=Lato:300italic,700italic,300,700);

body {
  padding:50px;
  font:14px/1.5 Lato, "Helvetica Neue", Helvetica, Arial, sans-serif;
  color:#777;
  font-weight:300;
}

h1, h2, h3, h4, h5, h6 {
  color:#222;
  margin:0 0 20px;
}

p, ul, ol, table, pre, dl {
  margin:0 0 20px;
}

h1, h2, h3 {
  line-height:1.1;
}

h1 {
  font-size:28px;
}

h2 {
  color:#393939;
}

h3, h4, h5, h6 {
  color:#494949;
}

a {
  color:#39c;
  font-weight:400;
  text-decoration:none;
}

a small {
  font-size:11px;
  color:#777;
  margin-top:-0.6em;
  display:block;
}

.wrapper {
  width:860px;
  margin:0 auto;
}

blockquote {
  border-left:1px solid #e5e5e5;
  margin:0;
  padding:0 0 0 20px;
  font-style:italic;
}

code, pre {
  font-family:Monaco, Bitstream Vera Sans Mono, Lucida Console, Terminal;
  color:#333;
  font-size:12px;
}

pre {
  padding:8px 15px;
  background: #f8f8f8;  
  border-radius:5px;
  border:1px solid #e5e5e5;
  overflow-x: auto;
}

table {
  width:100%;
  border-collapse:collapse;
}

th, td {
  text-align:left;
  padding:5px 10px;
  border-bottom:1px solid #e5e5e5;
}

dt {
  color:#444;
  font-weight:700;
}

th {
  color:#444;
}

img {
  max-width:100%;
}

header {
  width:270px;
  float:left;
  position:fixed;
}

header ul {
  list-style:none;
  height:40px;
  
  padding:0;
  
  background: #eee;
  background: -moz-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8f8f8), color-stop(100%,#dddddd));
  background: -webkit-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
  background: -o-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
  background: -ms-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
  background: linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
  
  border-radius:5px;
  border:1px solid #d2d2d2;
  box-shadow:inset #fff 0 1px 0, inset rgba(0,0,0,0.03) 0 -1px 0;
  width:270px;
}

header li {
  list-style-type: none;
}

header ul a {
  line-height:1;
  font-size:11px;
  color:#999;
  display:block;
  text-align:center;
  padding-top:6px;
  height:40px;
}

strong {
  color:#222;
  font-weight:700;
}

header ul li + li {
  width:88px;
  border-left:1px solid #fff;
}

header ul li + li + li {
  border-right:none;
  width:89px;
}

header ul a strong {
  font-size:14px;
  display:block;
  color:#222;
}

section {
  width:500px;
  float:right;
  padding-bottom:50px;
}

small {
  font-size:11px;
}

hr {
  border:0;
  background:#e5e5e5;
  height:1px;
  margin:0 0 20px;
}

footer {
  width:270px;
  float:left;
  position:fixed;
  bottom:50px;
}

@media print, screen and (max-width: 960px) {
  
  div.wrapper {
    width:auto;
    margin:0;
  }
  
  header, section, footer {
    float:none;
    position:static;
    width:auto;
  }
  
  header {
    padding-right:320px;
  }
  
  section {
    border:1px solid #e5e5e5;
    border-width:1px 0;
    padding:20px 0;
    margin:0 0 20px;
  }
  
  header a small {
    display:inline;
  }
  
  header ul {
    position:absolute;
    right:50px;
    top:52px;
  }
}

@media print, screen and (max-width: 720px) {
  body {
    word-wrap:break-word;
  }
  
  header {
    padding:0;
  }
  
  header ul, header p.view {
    position:static;
  }
  
  pre, code {
    word-wrap:normal;
  }
}

@media print, screen and (max-width: 480px) {
  body {
    padding:15px;
  }
  
  header ul {
    display:none;
  }
}

@media print {
  body {
    padding:0.4in;
    font-size:12pt;
    color:#444;
  }
}
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <title>Blog Title</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
	
    <!--[if lt IE 9]>
    <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
  
    <div class="wrapper">
	
	  <script type="text/x-handlebars">
	  <!-- Header which is on the right side. -->
	  <header>
		<h1> {{App.name}} </h1>
		<h2>{{App.tag}}</h2>
		<p> {{App.stub}}</p>

		<!-- Links to the main content -->
			
		<h3> Recent Content </h3> 
			<!-- Ember code to filter through five most recent posts --> 
			{{#each post in model}}
					<li> {{#linkTo "post" this}} {{post.title}} {{/linkTo}}</li>
			{{/each}}

	  </header>

	  <!-- Footer -->
      <footer>
        <p><small>Hosted on GitHub Pages &mdash; Theme by <a href="https://github.com/orderedlist">orderedlist</a> with heavy modifications from Jeel Shah</small></p>
      </footer>

      </script>
	  <!-- Main content for the day. The rest of the content will be on the left 
		   Ember code will surround this (?)
	  -->
	  	
	  	<script type="text/x-handlebars">
		  <section>
			<h3> Welcome to my blog </h3>
				<p>
					{{outlet}}
				</p>
		  </section>
		 </script>
    </div>
	
	<!-- javascripts -->
    
	<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
	<script src="//cdnjs.cloudflare.com/ajax/libs/ember.js/1.9.0-beta.1/ember.min.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
  </body>
</html>

我的尝试

我试图制作两个不同的车把脚本,一个是对帖子的引用,即

<script type="text/x-handlebars" id="posts">
          <section>
            <h3> Welcome to my blog </h3>
                <p>
                    {{outlet}}
                </p>
          </section>
         </script> 

但这不起作用。我还尝试围绕整个div创建一个单独的把手脚本,但这也不起作用。

如果有人能指出问题所在的正确方向:我会永远负债累累。谢谢! :)

更新 我已更新HTML/js的代码。可以查看here

1 个答案:

答案 0 :(得分:1)

你遗漏了一些关于余烬的想法 -
命名很重要。你有帖子 - 应该在模型中,然后使用灯具适配器来声明它们。 您有一个帖子模板但没有定义帖子路由或在路由器中。

this.resource("posts", {path: "/posts"}, function(){
    this.resource("post", {path: "/:post_id"});
});

路由器中没有索引路由。

this.resource("index", {path: "/"}, function() {});

你看起来根本就没有控制器。(虽然它现在看起来像你现在不需要它们)

如果您使用的是ember,我建议您使用ember-cli。它将帮助您设置应用程序结构并帮助命名约定。 http://www.ember-cli.com/

通过一个余烬教程(无论是在余烬网站本身还是在另一个网站上)也没有坏处。尝试做一个尽可能新的,因为代码移动速度太快,以至于教程很快就会过时。

链接

{{#each post in model}}
    <li> {{#link-to "post" post}} {{post.title}} {{/link-to}}</li>
{{/each}}

所以对于链接 - {{#link-to&#34; template&#34; model}}其中template是模板,model是你通过链接传递的项目。所以在你的情况下它的&#34; post&#34;模板,因为您在每个语句中将模型定义为post,所以您使用它。通过文档http://emberjs.com/guides/templates/links/#toc_the-code-link-to-code-helper