douzhuan1432 2014-08-08 17:21
浏览 51
已采纳

聚合物表达

I'm trying to use the Polymer expressions to show certain things.

I have a .php file that prints out a json_encoded array of for example names. Now there's 3 possibilities the way I see it:

  1. The list of names gets returned properly
  2. There are no names found in a database, so null or something is printed
  3. An error occured in the php file

So depending on what gets returned, I'd like to display the correcy message.

This can be done using <template if="{{conditionalValue}}">. This is what I have:

<template if="{{people != null}}">
    <template repeat="{{person in people}}">
        <name-card name="{{person.name}}">
        </album-card>
    </template>
</template>

<template if="{{people == null}}">
    <div>There are no people listed.</div>
</template>

This doesn't seem to work. When the .php file returns a list, the top templates both get executed and I get a whole list of <name-card> elements. However, when in the php file I do echo json_encode(null) for testing measures, nothing displays at all. So I must be doing something wrong with my conditionalValue. How do I test whether this is an array with elements in it?

Also, is there a way to test for non-json content, in case of an error on the php script?

  • 写回答

1条回答 默认 最新

  • dthh5038 2014-08-08 19:03
    关注

    Use for example the developer console of Chrome (or some other tool) to see what the call to your PHP script actually returns (because "null or something" is a bit vague.)

    Then at some point log the contents of the people property to see whats actually in it. I doubt that this property is really null. Presumably its something like "" or [] (or even "null").

    If people is indeed null, then your code works fine. One can verify this with a small test element that only contains the two templates and sets the people property directly in the created() function.

    In all other cases it depends. If the empty case is an empty array, you could write for example:

    <template if="{{people.length > 0}}">
    

    and

    <template if="{{people.length == 0}}">
    

    To your question regarding the non-json response error: it depends again. If you parse the JSON content with JSON.parse() yourself, then it will throw an exception in case of an error. If you use core-ajax, its current implementation catches the error, logs a warning and returns the XHR response text. So you may want to extend this element and overwrite the jsonHandler() function. Or use core-xhr and implement the JSON parsing yourself.

    The best way to handle errors is to always return a valid JSON response (with properties like "error" or "message") and use HTTP status codes. See this SO question for further reading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?