TaffyDB - 将数据呈现为HTML [英] TaffyDB - rendering data to HTML

查看:93
本文介绍了TaffyDB - 将数据呈现为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个简单的TaffyDB数据库:

Lets say you have a simple TaffyDB database:

var example = TAFFY([
                     {fruit:"apple", color:"green", taste:"sweet"},
                     {fruit:"banana", color:"yellow", taste:"more sweet"},
                     {fruit:"tomato", color:"red", taste:"like tomato"} 
                    ]);

如何将一个接一个的水果随机渲染为HTML:
我的解决方案为javascript初学者:

How would you render randomly one fruit after another to HTML: My solution as a javascript beginner:

var fruit = example().count();
var random =  Math.floor(Math.random()*count);
var fruit = example().select("fruit")[random];
var color = example().select("color")[random];
var taste = example().select("taste")[random];

$(document).ready(function(){
   $('#somediv').append("<p>" + fruit + "</p>");
   $('#somediv').append("<p>" + color + "</p>");
   $('#somediv').append("<p>" + taste + "</p>");
});

我认为这太复杂了。

会不会另一种解决方案是什么?

推荐答案

在TaffyDB 2.0中,你可以使用取代作为你讨论的部分内容。

In TaffyDB 2.0 you can use supplant for part of what you discussing.

我可能会编写如下代码:

I might author the code something like this:

var example = TAFFY([
                 {fruit:"apple", color:"green", taste:"sweet", order:0},
                 {fruit:"banana", color:"yellow", taste:"more sweet", order:0},
                 {fruit:"tomato", color:"red", taste:"like tomato", order:0} 
                ]);

$('#somediv').html(
    example().update(function () {
    this.order = Math.floor(Math.random() * 100);
    return this;
    }).order("order").supplant("<p>{fruit}</p>")
);

这篇关于TaffyDB - 将数据呈现为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆