Search code examples
javascriptjqueryforerunnerdb

Why doesn't ForerunnerDB finds my tag?


I have the following setup:

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Barebones todo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="./js/dist/fdb-all.min.js" type="text/javascript"></script>
<script src="./js/dist/fdb-autobind.min.js" type="text/javascript"></script>
<script src="scrypt.js" type="text/javascript"></script>
<script id="myLinkFragment" type="text/x-jsrender">
    <li data-link="id{:_id}">{^{:name}}</li>
</script>

</head>
<body>


<form onsubmit="addGoal(); return false;">
  <label for="goalinput">Enter goal:</label>
 <input type="text" id="goalinput">
  <input type="submit">
</form>
<button type="button" onclick="retrieve()">Retrieve</button>
<div id="p"></div>

<ul id="myList">
</ul>

</body>
</html>

and my scrypt.js file:

var fdb = new ForerunnerDB();
var db = fdb.db("myDatabaseName");
var goalCollection = db.collection("goal");

function addGoal() {
    console.log("addig goal...");
    var newgoalname = document.getElementById("goalinput").value;
    console.log(newgoalname);

    goalCollection.insert({
    name: newgoalname,
    order: 99
});

    };
function retrieve() {
    var findings = goalCollection.find({});
console.log(findings);


document.getElementById("p").innerHTML = findings;


}

goalCollection.link("#myList", "#myLinkFragment");

And then I receive and error telling:

fdb-autobind.min.js:1 Uncaught ForerunnerDB [Collection]goal Cannot bind collection to target selector "#myList" because it does not exist in the DOM!

I don't really understand the issue, because clearly there is a UL tag with the ID of myList. I was think maybe jquery is not invoked properly, but I used the documented way, so It shouldn't be a problem. I'm out of ideas what could cause the problem.


Solution

  • The reason the collection cannot bind is because the script executes before the DOM is loaded.

    You need to move tag:

    <script src="scrypt.js" type="text/javascript"></script>
    

    from the head tag to the end of the body tag.

    This will ensure that the DOM is in existence before your script executes.

    Source: I am the developer of ForerunnerDB