jasonkneen / RESTe

A simple JavaScript REST / API helper for Titanium
125 stars 34 forks source link

Can't find variable: #33

Closed adamtarmstrong closed 8 years ago

adamtarmstrong commented 8 years ago

I have read through your doc over and over for, literally, weeks and I cannot figure out what I'm doing wrong.

Error: "Can't find variable: retailers";

JS File

var reste = require("reste");
var api = new reste();
api.config({
    debug: true,
    autoValidateParams: false,
    validatesSecureCertificate: false,
    timeout: 4000,
    url: "http://demo6411181.mockable.io/valet/",
    requestHeaders: {
        "Content-Type": "application/json"
    },
    models: [{
        name: "retailers",
        id: "objectId",
        collections: [{
            name: "retailers",
            content: "results",
            read: "getRetailers"
        }],
    }],
    methods: [{
        name: "getRetailers",
        get: "me/retailers"
    }],
    onError: function(e, retry) {
        var dialog = Ti.UI.createAlertDialog({
            title: "Connection error",
            message: "There was an error connecting to the server, check your network connection and  retry.",
            buttonNames: ['Retry']
        });

        dialog.addEventListener("click", function() {
            retry();
        });
        dialog.show();
    },
    onLoad: function(e, callback) {
        callback(e);
    }
});

Alloy.Collections.retailers.fetch();

XML

<Alloy>
    <Window id="meWin">

       <TableView dataCollection="retailers">
                <TableViewRow model="{objectId}">
                    <Label class="subTitle" bottom="10" left="20" text="{retailer}"/>
                </TableViewRow>
    </TableView>

    </Window>
</Alloy>
jasonkneen commented 8 years ago

Where is your config located? Alloy.js?

adamtarmstrong commented 8 years ago

no. in the same controller file associated with the view. me_root.js (logic + reste config) me_root.xml view

jasonkneen commented 8 years ago

Ok that's your problem. You can't define the collections in the same controller / view. It's telling you it doesn't exist because the binding takes place before the JS code is run.

Move the JS code above to index.js or alloy.js so it's executed before the target controller is opened.

You only need one reste config so alloy.js is the best place.

adamtarmstrong commented 8 years ago

oh. so its more of a "timing" issue. Sorry, I had no idea. I just moved to alloy.js and it worked. Thank you, thank you, thank you so much!

jasonkneen commented 8 years ago

Kind of -- the way Alloy binding works anyway is to do the binding BEFORE the JS is run -- so you'd define your models / collections as per normal Alloy subfolders etc and then you can bind this to views -- the binding occurs before the JS runs.

So with RESTe, you need to define the config in the alloy.js or index.js (as long as index.js isn't binding anything) and then you're fine.

It's also why, if you want to say do binding of sub collections e.g. a "comments" array within a model, you have to do this BEFORE you open the comments binding view.

So if I have a collection of posts and then the user clicks one and I want to show the post and comments, I'd generate an on-the-fly collection on the click, in the post list controller BEFORE I open the view/controller that's binding the comments.

So yes, it's timing but it's all about how binding in Alloy works.