1

Trying to get a simple example with firebase and knockoutjs working. All i'm trying to do is take what is in firebase and bind it to my template in knockout. Sounds simple enough right? Well here is the code that's not working. I've looked it over but maybe I'm missing something. Oh this also makes use of knockoutfire.

    <!DOCTYPE html> 
<html> 
<head>
<title>knockout</title>

</head>

<body>


<div id="viewModel">
    <ul data-bind="foreach: chat">
        <li data-bind="text: nick"></li>
    </ul>
</div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>
<script type="text/javascript" src="knockout.js"></script>
<script type='text/javascript' src='https://cdn.firebase.com/v0/firebase.js'></script>
<script type="text/javascript" src='knockoutFire/knockoutFire.js'></script> 
<script type="text/javascript" src='model.js'></script>

</body>
</html>

and the model.js:

var firebase = new Firebase("https://kingpinapp.firebaseio.com");
var viewModel =  KnockoutFire.observable(firebase, {
chat: {
    nick: true,
}
});
ko.applyBindings(viewModel, document.getElementById("viewModel"));

if I'm some how getting the model view wrong checkout the firebaseio link to see how the data is laid out. All I get when I visit index.html is a list with nothing in it. Just a bullet point, nothing else.

EDIT: just realised no one else can see my data. Well here is the JSON downloaded from the url:

  {
  "chat" : {
    "nick" : "hello"
  }
}
4

1 回答 1

1

我认为你必须使用with: chat而不是foreach: chat

<ul data-bind="with: chat">

如果您需要foreach绑定,firebase 中的数据如下所示;

{
  "chat": {
    "-XXX": {"nick": "hello"},
    "-YYY": {"nick": "hi"}
  }
}

和代码:

var viewModel =  KnockoutFire.observable(firebase, {
  chat: {
    "$chat": {
      nick: true,
    }
  }
});
于 2013-04-27T02:10:29.587 回答