Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question regarding passing additional args #7

Closed
swampthang opened this issue Apr 26, 2017 · 5 comments
Closed

Question regarding passing additional args #7

swampthang opened this issue Apr 26, 2017 · 5 comments
Labels

Comments

@swampthang
Copy link

I have a need to pass additional data (not in an nSQL table) to be used, for example, after a result is pulled using a query. I have the following example...

var extraData = {var1: "one", var2: "two", var3: "three"};

nSQL('aniblocks')
  .query("select")
  .where(["id","=",block.settings.pk])
  .exec()
  .then(function(result,db){
    // use data from extraData here along with the result
  })

Is there a convenient way to pass additional data? Currently handling it by creating a function and calling it like:

var extraData = {var1: "one", var2: "two", var3: "three"};
function getExtraData() {
  return extraData;
}
nSQL('aniblocks')
  .query("select")
  .where(["id","=",block.settings.pk])
  .exec()
  .then(function(result,db){
    var data = getExtraData();
    // now work with the data
  })
@only-cliches
Copy link
Owner

What's the relationship to the extra data? Is it tied in anyway to the data being pulled from the database?

@swampthang
Copy link
Author

swampthang commented Apr 27, 2017

No. It's strictly ancillary data that needs to be tweaked based on the nSQL data. Just wondered if there might be a need for some pass-through data as an option. The main reason is because once you enter the promises for nSQL nothing outside the scope of nSQL is available even though it might be scoped inside a function that contains the nSQL calls.

@only-cliches
Copy link
Owner

There's an undocumented property on the nSQL variable that can hold arbitrary data:

nSQL().data = {something:"here"};
console.log(nSQL().data.something): // <- "here"

The variable isn't used by the lib at all, so it's up to you to initialize it and use it how ever.

Beyond that, since the data isn't related to the nSQL data it kind of makes sense to manage that outside of the nSQL store, just my two cents.

@only-cliches
Copy link
Owner

Also, scope usually allows you to access parent scoped variables inside the promise.

var globalVar = "yo".
nSQL("table").query("select").exec().then(function(rows, db) {
   console.log(globalVar) // <- "yo"
});

There's a really good article on Medium that covers JS closures, might make this make more sense.

@swampthang
Copy link
Author

ok, thanks a bunch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants