0

I have a javascript module that neatly separates data from dom and returns a public api for its data and ui objects:

var PtCalcApp = (function() {
    var ptCalc = ptCalc || {};

    ptCalc.ui = {
        storage: $('#pt-storage'),
        backup: {
            daily: $('#per-day-data'),
            weekly: $('#per-week-data'),
            monthly: $('#per-month-data'),
            yearly: $('#per-year-data')
        },
        change: {
            yearly: $('#annual-change'),
            daily: $('#daily-change')
        }
    };

    ptCalc.data = {
        storage: function() {
            ptCalc.ui.storage.val()
        }
    }

    return ptCalc;
})();

Now when I try to access data like this this:

PtCalcApp.data.storage()

it returns undefined. What am I doing wrong with this self invoking function pattern? How do I fix this?

4
  • You are storing the data directly in the DOM in visual format, I don't see the separation. Commented Jun 19, 2013 at 10:21
  • Ok can you show me a better way to do this without using a mvc framework? Commented Jun 19, 2013 at 10:21
  • 1
    This doesn't look like a valid selector $('pt-storage') Commented Jun 19, 2013 at 10:22
  • You are right - that fixed the ui access issue - but now I am still not getting my storage input data when I call PtCalcApp.data.storage() - I get undefined Commented Jun 19, 2013 at 10:26

2 Answers 2

1

You are not returning anything from the function. Without a return statement, the function will return undefined (unless called with new, but that's a different matter).

storage: function() {
    ptCalc.ui.storage.val()
}

should be

storage: function() {
    return ptCalc.ui.storage.val()
}
Sign up to request clarification or add additional context in comments.

Comments

0

PtCalcApp.ui.storage is not a function, it is a property.

You need to access it using PtCalcApp.ui.storage not as PtCalcApp.ui.storage()

If you want to use a a function then you need to declare it as

storage: function(){
    return $('pt-storage');
}

3 Comments

Sorry, I made mistake in posting the code I was accessing it as PtCalcApp.ui.storage - It doesn't work
@AmitErandole looks like you are missing # in front of pt-storage
You are right - I fixed that but there is still the problem with not getting my data back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.