如何获取调用它的时候利用的数据? [英] How to make use of the data when calling fetch on it?

查看:138
本文介绍了如何获取调用它的时候利用的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与骨干网和backbone.localstorage require.js,我试图找出如何利用数据的调用获取,不知道如何去后...我试图通过数据进入我的观点,并利用它。

下面是存储在localStorage的数据的示例:

  [{艺术家:点击数1娱乐4-1-1,称号:听到凯蒂的佩里的新专辑},...]

因此​​,它是一个阵列内的对象。

这是我的code骨干...

  VAR芝=新歌();
songz.localStorage =新Backbone.LocalStorage(音乐);
songz.fetch({数据类型:'JSON'});
VAR songV =新SongV({集:松芝});
songV.render();

歌曲是一个集合,看起来像这样在集合文件,SongV是为每首歌曲的看法。

下面是与code以上的观点包括:

 定义([
  jQuery的,
  下划线,
  '骨干',
  模型/歌曲,
  收藏/歌曲',
  意见/歌曲,
  的文字!模板/ page.html即可]函数($,_,骨干,宋,歌曲,SongV,佩吉特){  VAR页= Backbone.View.extend({    EL:$(#页),
    渲染:功能(){
      这$ el.html(帕吉特);      VAR松芝=新歌();
      songz.localStorage =新Backbone.LocalStorage(音乐);
      songz.fetch({数据类型:'JSON'});
      VAR songV =新SongV({集:松芝});
      songV.render();    }
  });
    返回页面;
});

下面是收集的文件:

 定义([
  jQuery的,
  下划线,
  '骨干',
  模型/歌曲,
]函数($,_,骨干,宋){    VAR歌曲= Backbone.Collection.extend({
        型号:宋,    初始化:功能(){
    }
    });
  返回歌曲;
});

下面是模型文件:

 定义([
  下划线,
  '骨干'
],功能(_,骨干){  VAR歌= Backbone.Model.extend({  });
  返回宋;
});

下面是模板文件:

 < TR>
    <第i个数字与LT; /第i
    <第i个标题< /第i
    <第i个艺术家< /第i
    <第i个DATE_ADDED< /第i
    <第i个影片版权< /第i
< / TR>
  <%_.each(听歌,功能(歌曲){%GT;
  &所述; TR>
    &所述; TD> <%= song.get(数字)%GT; < / TD>
    &所述; TD> <%= song.get(标题)%> < / TD>
    &所述; TD> <%= song.get(艺术家)%GT; < / TD>
    &所述; TD> &所述;%= song.get(ADDED_ON)%> < / TD>
    &所述; TD> <%= song.get(视频)%> < / TD>
  < / TR>
  &所述;%}); %GT;


解决方案

您需要获取并绑定到集合中的复位事件看,当它被成功地从服务器提取的数据。

 页= Backbone.View.extend
 EL:$('#页)
 渲染: - >
  松芝=新歌曲()
  #初始化视图
  songV =新SongV({集:松芝})
  #呈现视图
  songV.render()
  #取集合
  songz.fetch()SongV = Backbone.View.extend
  初始化: - >
    @listenTo @collection,复位,@onReset
  onReset:(集合) - GT;
    #使用填充收集数据
    ...歌曲= Backbone.Collection.extend
  型号:宋
  localStorage的新Backbone.LocalStorage(音乐)
  初始化: - >

这是如何存储Backbone.LocalStorage集合和模型 - > 这里结果
请参阅表底部,为手拉葫芦,关键是你本地存储的名称,然后每个模型都有一个独特的密钥。

因此​​,这意味着,如果你有数据坐在本地存储,你已经把自己的存在,你应该把它拿出来与喜欢的 store.js ,然后用它来填充Backbone.Collection。

另外,你可以从服务器获取(推荐),将填充您的收藏。或者你可以引导在页面加载和重置您的收藏中的数据的方式。

I am using require.js with backbone and backbone.localstorage and I am trying to figure out how to make use of the data after calling fetch, not sure how to go about it... I am trying to pass the data into my view and make use of it.

Here is the example of the data stored in localstorage:

[{"artist":"Hits 1 Entertainment 4-1-1","title":"Hear Katy's Perry's New Album!"}, ...]

So it is objects within an array.

This is my code for backbone...

var songz = new Songs();
songz.localStorage = new Backbone.LocalStorage("music");
songz.fetch({dataType: 'json'});
var songV = new SongV({collection: songz});
songV.render();

Songs is a collection, that looks like this in the collections file, SongV is the view for each song.

Here is the view with the code above included:

define([
  'jquery',
  'underscore',
  'backbone',
  'models/song',
  'collections/songs',
  'views/song',
  'text!templates/page.html'

], function($, _, Backbone, Song, Songs, SongV, PageT){ 

  var Page = Backbone.View.extend({

    el: $("#page"),
    render: function () { 
      this.$el.html( PageT );

      var songz = new Songs();
      songz.localStorage = new Backbone.LocalStorage("music");
      songz.fetch({dataType: 'json'});
      var songV = new SongV({collection: songz});
      songV.render();



    }


  });
    return Page;
});

Here is the collection file:

define([
  'jquery',
  'underscore',
  'backbone',
  'models/song',
], function($, _, Backbone, Song){

    var Songs = Backbone.Collection.extend({
        model: Song,

    initialize: function () {
    }
    });
  return Songs;
});

Here is the model file:

define([
  'underscore',
  'backbone'
], function(_, Backbone) {

  var Song = Backbone.Model.extend({

  });
  return Song;
});

Here is the template file:

<tr>
    <th> Number </th>
    <th> Title </th>
    <th> Artist </th>
    <th> Date_Added </th>
    <th> Video </th>
</tr>
  <% _.each(songs, function(song){ %>
  <tr>
    <td> <%= song.get("number") %> </td>
    <td> <%= song.get("title") %> </td>
    <td> <%= song.get("artist") %> </td>
    <td> <%= song.get("added_on") %> </td>
    <td> <%= song.get("video") %> </td>
  </tr>
  <% }); %>

解决方案

You need to fetch and then bind to the reset event on the collection to see when it was successfully pulled the data from the server.

Page = Backbone.View.extend
 el: $('#page')
 render: ->
  songz = new Songs()
  # Initialize view
  songV = new SongV({collection: songz})
  # Render view
  songV.render()
  # Fetch collection
  songz.fetch()

SongV = Backbone.View.extend
  initialize: ->
    @listenTo @collection, "reset", @onReset
  onReset: (collection) ->
    # Use populated collection data
    ...

Songs = Backbone.Collection.extend
  model: Song
  localStorage: new Backbone.LocalStorage("music")
  initialize: ->

This is how Backbone.LocalStorage stores the collections and models --> here
See the table at the bottom, the key for the chain block is you local storage name and then each model has a unique key.

So this means if you have data sitting in local storage that you have put there yourself, you should take it out with a cross-browser local storage device like store.js and then use it to populate your Backbone.Collection.

Alternatively, you could fetch from the server (recommended) and that will populate your collection. Or you could bootstrap the data on page load and reset your collection that way.

这篇关于如何获取调用它的时候利用的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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