小编Mar*_*son的帖子

AngularJS $资源创建新对象而不是更新对象

// Set up the $resource
$scope.Users = $resource("http://localhost/users/:id");

// Retrieve the user who has id=1
$scope.user = $scope.Users.get({ id : 1 }); // returns existing user

// Results
{"created_at":"2013-03-03T06:32:30Z","id":1,"name":"testing","updated_at":"2013-03-03T06:32:30Z"}

// Change this user's name
$scope.user.name = "New name";

// Attempt to save the change 
$scope.user.$save();

// Results calls POST /users and not PUT /users/1
{"created_at":"2013-03-03T23:25:03Z","id":2,"name":"New name","updated_at":"2013-03-03T23:25:03Z"}
Run Code Online (Sandbox Code Playgroud)

我希望这会导致带有更改属性的PUT到/ users/1.但它改为POST/users,它会创建一个新用户(新ID和新名称).

有什么我做错了吗?

AngularJS v1.0.5
Run Code Online (Sandbox Code Playgroud)

rest angularjs

7
推荐指数
1
解决办法
7820
查看次数

哪个是更好的Javascript对象模式

哪个是更好的Javascript对象模式......

function dog(name) {
  this.name = name;
  this.getName = function() {
    return this.name;
  };
};
Run Code Online (Sandbox Code Playgroud)

要么

function cat(name) {
  this.name = name;
};
cat.prototype.getName = function() {
  return this.name;
};
Run Code Online (Sandbox Code Playgroud)

为什么?

-----编辑

一个或另一个使用更多内存吗?

是一个或多或少"CPU"密集比另一个?

哪个更易于维护?

哪个更具可扩展性?

哪个更具可读性?

javascript object

6
推荐指数
2
解决办法
1327
查看次数

小胡子在服务器(rails)和客户端(javascript)上呈现

在服务器(使用rails)和客户端(使用javascript)上使用时,是否有关于Mustache最佳实践的文档?

# hello_world.mustache
Hello {{planet}}

# some other file
<%
hello_world_template = File.read(File.dirname(__FILE__) + "/hello_world.mustache")
%>

<script id="hello_world_template" type="text/x-jquery-tmpl"> 
    <%= hello_world_template %>
</script>

<script>
    // $.mustache = using mustache.js and a jquery mustache wrapper 
    // search on "Shameless port of a shameless port"
    document.write($.mustache($("#hello_world_template").html(), { "planet" : "World!" }));
</script>

<%= Mustache.render(hello_world_template, :planet => "World!") %>
Run Code Online (Sandbox Code Playgroud)

以上是不可扩展的.我不想为此制作自己的发动机.

是否有更完整的模板引擎允许在服务器和客户端上重用模板?

另外,还有一个可以解释服务器和客户端上的嵌套模板吗?

javascript ruby-on-rails mustache

4
推荐指数
1
解决办法
3382
查看次数

Ember.js itemController和模型和控制器混淆

让我们假设我已经正确设置了一切.我有一个模型App.User,我有一个控制器App.UsersIndexUserController.


好的好的好

以下视图模板......

<script type="text/x-handlebars" data-template-name="users_index_template">
  {{#each user in users}}
    {{log user}}
    {{#linkTo users.user user}}{{user.name}}{{/linkTo}}
  {{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)

...这将在浏览器的console.log中输出以下内容...

<App.User:ember258> { created_at="2013-03-05T01:51:15Z", id=76 ... etc ... }
Run Code Online (Sandbox Code Playgroud)

坏坏不好

但是,itemController在我的模板中使用指令时,就像这样......

<script type="text/x-handlebars" data-template-name="users_index_template">
  {{#each user in users itemController="usersIndexUser"}}
    {{log user}}
    {{#linkTo users.user user}}{{user.name}}{{/linkTo}}
  {{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)

...这将在浏览器的console.log中输出以下内容...

<App.UsersIndexUserController:ember253> { target=<Ember.ArrayController:ember245> ... etc ... }
Run Code Online (Sandbox Code Playgroud)

我期待为这两种情况{{log user}}返回一个实例App.User.但正如您在上面所看到的,它返回一个App.UsersIndexUserController使用itemController指令的实例,并App.User在不使用itemController指令时返回和实例.

我应该App.UsersIndexUserController明确地返回一些对象,以便在上述两种情况下,{{log user}}都会返回App.User吗?

App.UsersIndexUserController …
Run Code Online (Sandbox Code Playgroud)

ember.js

1
推荐指数
1
解决办法
2038
查看次数