TaffyDB - rendering data to HTML

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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"}                      ]); 

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>"); }); 

I think that this is too complicated.
Would there be another solution to this?

回答1:

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>") ); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!