我正在开发一个Polymer应用程序,它从RESTful API中提取数据并使用它构建接口.我在概念上坚持的一个特定领域是http://www.polymer-project.org/docs/polymer/polymer.html#global中描述的Monostate模式的实现.实际上,我可以将声明性属性添加到新组件app-globals中,然后合理地直接访问它.
以下是关键问题:如果我通过core-ajax将数据来回(并且可能重新提交)到app-globals组件中的API,我该如何确保app-globals组件的所有使用者都具有相同的数据?如果我使用建议的模式,似乎我已经失去了我的单调性:
<polymer-element name="my-component">
<template>
<app-globals id="globals"></app-globals>
<div id="firstname"></div>
<div id="lastname"></div>
</template>
<script>
Polymer('my-component', {
ready: function() { this.globals = this.$.globals; }
});
</script>
</polymer-element>
Run Code Online (Sandbox Code Playgroud)
因为每个使用app-globals的组件都会提取自己的API数据版本.我错过了什么吗?还有另一种方法可以确保应用程序始终只有一个版本的真相吗?
我正在使用core-ajax来检索JSON数据.转动组件以作为JSON回发到服务器是另一回事.在所有情况下,无论传入的contentType或handleAs参数如何,看来我作为输入传入的JSON对象正在转换回服务器头中的key = value.
代码:
var ajax = document.querySelector('core-ajax');
ajax.method = 'POST';
ajax.handleAs = 'JSON';
ajax.contentType = 'application/json';
ajax.params = JSON.stringify(data);
ajax.go();
Run Code Online (Sandbox Code Playgroud)
真的很直白.Go中的日志给我:
2014/07/22 14:23:09 utils.go:139: OPTIONS /1/users/173?access_token=(token)
2014/07/22 14:23:09 utils.go:124: POST /1/users/173?access_token=(token)
2014/07/22 14:23:09 users.go:379: full_name=Greg%20Johnson
Run Code Online (Sandbox Code Playgroud)
我们已经确认我们方面没有发生任何转变.请求标题出来就好了.
我完全可能会遗漏一些东西.我们怎样才能成功发布JSON数据?