【问题标题】:Backbone.js: I am using web service call to get collection object using Backbone.js, How to display the collection response object data in html?Backbone.js:我正在使用 Web 服务调用使用 Backbone.js 获取集合对象,如何在 html 中显示集合响应对象数据?
【发布时间】:2015-04-14 11:47:48
【问题描述】:

我在 Backbone.js 脚本中使用 Web 服务调用来使用以下代码在控制台中获取集合对象响应:

<script>
$(document).ready(function(){

var pgServerName =$("#serverName").text();
var pgPort =$("#serverPort").text();
var pgProjectName =$("#projectName").text();
var userLogged =$("#loggedInUser").text();

$("button").on('click', function(){

     Server=Backbone.Model.extend({});

     ServerList= Backbone.Collection.extend(
        {
            model:Server,
            url:"/MicroStrategy/servlet/taskProc?taskId=getAllUserDecks&taskEnv=xhr&taskContentType=json&iServer="+pgServerName+"&port="+pgPort+"&userId="+userLogged+"&authMode=64&projName="+pgProjectName,
            initialize:function(){
            alert('In collection init');
            }
        }
);

    list=new ServerList;
    list.fetch({
    success:function(collection, response)
    {
        alert(response);
        console.log(response);
    },

    error:function(){alert("error");}
});
});

});

我在控制台中收到以下响应

 "deckIds": Array[3] [
    0 Object {
      "port": "30170",
      "deckName": "Interactive Sandbox",
      "projName": "CFO Sandbox",
      "createdDateTime": "3/5/2015 11:39:36 AM",
      "deckId": "12",

    },
    Object 1 {
      "port": "30170",
      "deckName": "Standard Management Reporting",
      "projName": "Standard Management Reporting",
      "createdDateTime": "2/18/2015 8:15:14 AM",
      "deckId": "1",

    },
    Object 2{
      "port": "30170",
      "deckName": "Month End Tracking Reports",
      "projName": "Standard Management Reporting",
      "createdDateTime": "2/12/2015 7:24:01 AM",
      "deckId": "3",

    },

现在我想在下面的代码中以 html 格式显示 deckId、createdDateTime、deckName

<table id="example">
                        <thead>
                            <tr>

                                <th>deckId</th>
                                <th>deckName</th>
                                <th>createdDateTime</th>                                            


                            </tr>
                        </thead>

                        <tbody>


                            <tr>                            

                                <td>actual data from deckId</td>
                                <td>actual data from deckName</td>
                                <td>actual data from createdDateTime </td>                          


                            </tr>

                        </tbody>
</table>

如何在 html 页面上显示我在控制台中得到的响应?请帮忙

【问题讨论】:

  • 您需要确切的主干代码还是需要知道如何完成?
  • 我需要可以在现有主干脚本代码中附加并在 html 中调用的代码。确切的代码或任何参考也可以。
  • @Kushal 看看你能不能帮我解决这个问题。
  • 有多种方法。第一个也是我最不喜欢的是使用 jQuery 循环对象并将数据附加到 html 中(使用append)。其他更优雅的(在我看来)将使用模板引擎,例如 Mustache.jshandlebars.js,它们提供了一种非常好的显示数据的方式......请谷歌了解更多信息 :)
  • @KartikRao,您好,您能提供执行此通话的代码吗?我也有同样的问题,但是不知道怎么弄,谢谢

标签: javascript json backbone.js collections underscore.js


【解决方案1】:

至于我的理解,你是 Backbone.js 的新手,所以我会要求你阅读一些教程,而不是仅仅让其他人编写代码,然后将这段代码添加到你现有的代码中。

这里是链接:Backbone.js tutorials and learning resources

如果您有任何疑问,我很乐意在这种情况下为您提供帮助。 希望你能听从我的声音,学习而不是复制。

【讨论】:

    【解决方案2】:

    当您使用 Backbone 时,您可以只使用下划线模板引擎,因为 它是 Backbone 的依赖项。你可以在这里阅读更多相关信息 -> How to use underscore.js as a template engine?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 2011-12-19
      • 2011-08-23
      相关资源
      最近更新 更多