【问题标题】:How to fetch JSON data and update html using BackboneJs?如何使用 BackboneJs 获取 JSON 数据并更新 html?
【发布时间】:2013-12-04 03:48:15
【问题描述】:

我正在使用 BackgridJs,它使用 Backbonejs 用数据填充网格。

这就是我的代码现在的样子:

<!DOCTYPE html>
<html>
  <head>
    <title>Terminal</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../js/backgrid/lib/backgrid.css" />
    <script src="../js/backgrid/assets/js/jquery.js"></script>
    <script src="../js/backgrid/assets/js/underscore.js"></script>
    <script src="../js/backgrid/assets/js/backbone.js"></script>
    <script src="../js/backgrid/lib/backgrid.js"></script>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>Hello, world!</h1>

    <div class="btn-group">
    <a class="btn btn-ledger dropdown-toggle" data-toggle="dropdown" href="#">
    Ledger
    <span class="caret"></span>
    </a>
    <ul class="dropdown-menu">
    <li><a tabindex="-1" href="#">With Margin</a></li>
    <li><a tabindex="-1" href="#">Without Margin</a></li>
    </ul>
    </div>
    <div class="btn-group">
    <a class="btn btn-company-code dropdown-toggle" id = "company_code" data-toggle="dropdown" href="#">
    Company Code
    <span class="caret"></span>
    </a>
    <ul class="dropdown-menu">
    <li><a tabindex="-1" href="#">DHA</a></li>
    <li><a tabindex="-1" href="#">HAC</a></li>
    <li><a tabindex="-1" href="#">DHA</a></li>
    </ul>
    </div>
    <div class="btn-group">
    <a class="btn btn-cost-centre dropdown-toggle" data-toggle="dropdown" href="#">
    Cost centre
    <span class="caret"></span>
    </a>
    <ul class="dropdown-menu">
    <li><a tabindex="-1" href="#">ALL</a></li>
    <li><a tabindex="-1" href="#">NSE-EQ</a></li>
    </ul>
    </div>
    <div class="btn-group">
    <a class="btn btn-type dropdown-toggle" data-toggle="dropdown" href="#">
    Type
    <span class="caret"></span>
    </a>
    <ul class="dropdown-menu">
    <li><a tabindex="-1" href="#">CLNT</a></li>
    <li><a tabindex="-1" href="#">GENL</a></li>
    <li><a tabindex="-1" href="#">MARG</a></li>
    </ul>
    </div>

    <button type="submit" id = "submit" class="btn btn-success">
                <i class="icon-circle-arrow-right icon-large"></i> Submit
    </button>

    <div id="example-1-result" class="backgrid-container"></div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="../static/js/bootstrap.min.js"></script>

    <script>

    var Territory = Backbone.Model.extend({});

var Territories = Backbone.Collection.extend({
  model: Territory
  //url: "examples/territories.json"
});

//var territories = new Territories();
var territory1 = new Territory({ name: "How Bizarre", pop: 20 });
var territory2 = new Territory({ name: "How ", pop: 21 });
var territories = new Territories([territory1,territory2]);
var columns = [{
  name: "id", // The key of the model attribute
  label: "ID", // The name to display in the header
  editable: false, // By default every cell in a column is editable, but *ID* shouldn't be
  // Defines a cell type, and ID is displayed as an integer without the ',' separating 1000s.
  cell: Backgrid.IntegerCell.extend({
    orderSeparator: ''
  })
}, {
  name: "name",
  label: "Name",
  // The cell type can be a reference of a Backgrid.Cell subclass, any Backgrid.Cell subclass instances like *id* above, or a string
  cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
}, {
  name: "pop",
  label: "Population",
  cell: "integer" // An integer cell is a number cell that displays humanized integers
}, {
  name: "percentage",
  label: "% of World Population",
  cell: "number" // A cell type for floating point value, defaults to have a precision 2 decimal numbers
}, {
  name: "date",
  label: "Date",
  cell: "date",
}, {
  name: "url",
  label: "URL",
  cell: "uri" // Renders the value in an HTML anchor element
}];

// Initialize a new Grid instance
var refreshgrid = function(){
    var grid = new Backgrid.Grid({
      columns: columns,
      collection: territories
    });

    // Render the grid and attach the root to your HTML document
    $("#example-1-result").append(grid.render().$el);
}
// Fetch some countries from the url
//territories.fetch({reset: true});


    $(".dropdown-menu li").click(function(){
    $(this).closest('.btn-group').children('a').text($(this).text())
});

    $('#submit').on('click', function(event) {
        url = '/ledger/data'
        company_code = $("#company_code").text();

 // event.preventDefault(); // To prevent following the link (optional)
 $.post( url, { name: "John", time: "2pm" },function(data){
    ledgerData = JSON.parse(data);

    refreshgrid();
 });
      //$.post(url,function(response){
      //  alert(response);
      //});
});
    </script>
  </body>
</html>

当我在 Post 请求中获取 JSON 数据时,我无法弄清楚如何在 refreshgrid() 调用期间将其提供给网格。

在大多数 Backbonejs 示例中,url 在模型本身中,但对我来说,每次提交后 url 都会不断变化。那么,如何获取数据并将其提供给网格?

【问题讨论】:

    标签: javascript html json backbone.js backgrid


    【解决方案1】:

    默认情况下,Backbone.js 使用 Accept 标头为 application/json 发出 RESTful 请求。

    如果您在 Collection 上设置 url 属性,则获取数据很简单,只需调用 fetch

    在此处阅读有关Collection.url 属性的更多信息。

    var CandiesCollection = Backbone.Collection.extend({url: "/candies"});
    
    var c = new CandiesCollection;
    c.fetch();
    
    // GET /candies (Accept: application/json, text/javascript, */*; q=0.01)
    

    更多 Backbone 为你做神奇工作的例子!

    • 当您保存新模型(或使用collection.create())时,Backbone.js 会自动发出POST 请求

    • 同样,当您在现有模型上调用 model.save() 时,它会自动发出 PUT 请求。

    • model.destroy() 将发出DELETE 请求。


    除此之外,更新/刷新完全一样。

    【讨论】:

    • 如何发送post请求?
    • @Hick,我已经更新了我的答案以指出 Backbone 何时提出其他一些请求。关键是,你不必担心它。 Backbone 总是发出正确的请求。
    猜你喜欢
    • 2019-09-16
    • 2018-07-03
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 2022-10-12
    • 2013-05-18
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多