3

不支持的操作:不能在反射中使用 ownerName,因为它不包含在 @MirrorsUsed 注释中。

ownerName只是聚合物元素上的已发布属性。我知道有一些事情(在网上,而不是在这里)像这样,但没有一个可靠的答案......

我也在它下面得到这个:

NoSuchMethodError:找不到方法:'Symbol("title")'

任何人都有任何想法。已经为此挣扎了 3 个小时,准备倾倒聚合物。虽然它在 dartium 中很有趣,但如果它不能转换为 JS,我认为它没有真正的用处。

import 'package:polymer/polymer.dart';
import 'package:google_postendpoint_v1_api/postendpoint_v1_api_browser.dart';
import 'package:google_postendpoint_v1_api/postendpoint_v1_api_client.dart';
import 'dart:math';
//import 'package:intl/intl.dart';


/**
 * A Polymer post element.
 */
@CustomTag('post-element')
class SoberSky extends PolymerElement {
  @published int count = 0;
  @published String ownerName = "Casey";
  @published int ownerId = 888;
  @published int postId = 333;
  @published String content = "yo ho ho, and a bottle of rumsky!";
  @published String postContent = "This is an example of post content";

  @published int getOwnerId = 333;
  @published CollectionResponse_Comment listComment;
  @published CollectionResponse_Post listPost;
  @published String listCommentHTML;
  @published Post currentPost;
  @published bool yes = false;
  Postendpoint endpoint = new Postendpoint();
  var rng = new Random();

  var commentList;

  SoberSky.created() : super.created() {
    endpoint.rootUrl = "http://localhost:8888/"; // set the root URL
  }

  void getListComment([int tempPostId]) {
    if (tempPostId == null) {
      tempPostId = postId;
    }
    endpoint.listComment( tempPostId ).then((CollectionResponse_Comment commentCollection){
          this.listComment = commentCollection;
    }).catchError((e) => handleError(e));
  }

  void getPost() {
    endpoint.getPost( postId ).then((Post loadedMessage) {
      currentPost = loadedMessage; 
      getListComment(currentPost.key);
    }).catchError((e) => handleError(e));
  }

  void submitComment() {
    int id = rng.nextInt(1000);
    Comment comment = new Comment.fromJson({});
    comment.id = id;
    comment.content = content;
    comment.postId = postId;
    comment.ownerName = ownerName;
    comment.ownerId = ownerId;

    endpoint.insertComment( comment ).then((Comment savedComment) => endpoint.getComment(id)).
      then((Comment loadedMessage) {
        print(loadedMessage.content);
        getListComment(loadedMessage.postId);
      }).catchError((e) => handleError(e));  
  }

  void submitPost(){
    postId = rng.nextInt(1000);
    Post post = new Post.fromJson({});
    post.key = postId;
    post.ownerName = ownerName;
    post.ownerId = ownerId;
    post.title = postContent;

    endpoint.insertPost( post ).then((Post savedPost) => endpoint.getPost(postId)).
    then((Post loadedMessage) {
      print(loadedMessage.title);
      getPost();
      getListComment(loadedMessage.key);
    }).catchError((e) => handleError(e));
  }

  void handleError(Error e) {
    print("We had an error: ");
    print(e.toString());
  }

  @override
  void attached() {
  }
}

HTML 聚合物元素

<polymer-element name="click-counter" attributes="count">
  <template>
    <form action="javascript:void(0);" >
      <div class="entry">
        <label>Enter Post:</label>
        <input id="subject" type="text" value="{{postContent}}" size="45" maxlength="255">
        <button on-click="{{submitPost}}" class="btn btn-success" >Submit Post</button>    <br>

      </div>
    </form>
    <div class="post">  
      <h3>{{ currentPost.ownerName }}</h3>
      <p>{{ currentPost.title }}</p>
      <p>This is the postId: {{ currentPost.key }}</p>
  <p class="timestamp">{{ currentPost.uploadTime }}</p>
  <template repeat="{{comment in listComment.items}}">
    <div class="comment">
      <h3>{{ comment.ownerName }}</h3>
      <p>{{ comment.content }}</p>
      <p>This is the commentId: {{ comment.id }}</p>
      <p class="timestamp">{{ comment.formatDate }}</p>
        </div>
      </template>
      <form action="javascript:void(0);">
        <label>Enter Comment:</label>
        <input id="subject" type="text" value="{{content}}" size="45" maxlength="255">
            <button on-click="{{submitComment}}" class="btn btn-success">Submit Comment</button>    <br>
      </form>  
     </div>  
   </template>
  <script type="application/dart" src="clickcounter.dart"></script>  
</polymer-element>
4

1 回答 1

4

我还没有看到你的Unsupported operation消息。也许最近发生了一些变化。当类的NoSuchMethodError属性仅由聚合物表达式 (HTML) 引用时,这种情况很常见,因为摇树会丢弃所有未引用的代码,并且尚未为此评估聚合物表达式。@MirrorsUsed注释有助于弥合这一差距。

问题出在您的Post班级,因为它的属性仅在聚合物表达式中引用

<div class="post">  
  <h3>{{ currentPost.ownerName }}</h3>
  <p>{{ currentPost.title }}</p>
  <p>This is the postId: {{ currentPost.key }}</p>
  <p class="timestamp">{{ currentPost.uploadTime }}</p>

要在currentPost更改属性时更新您的视图,您应该使您的Post类像

class Post extends Object with Observable {
  @observable String ownerName;
  @observable String title;
  ...
}

如果您有 , 之类的注释@reflectable@published或者@observable您不需要@MirrorsUsed.

于 2013-12-31T11:01:09.207 回答