Per request variables in a node module? (express view helpers) - node.js

I am working on this project https://github.com/tanema/express-helpers that I forked fixed up and added new functionality to.
What I want to do it, instead of having to use form_tag_end() or even end_tag('tagname') I would just like to use a end() method. For this to work I need some sort of stack implementation for when a start tag is issued push onto the stack ect. I cannot just make a variable in the module called tagStack and just use that because it would create a race condition where the module is being used at the same time by two requests and the stack order gets messed up.
I was thinking if there was some way to have access to the request variable I would just store it in that and delete the variable if empty but I can't figure out how to do that.
Any suggestions?

Create your variable within a closure; it will be available within the scope of the instance, but not outside the instantiation of the functions, and will be garbage collected when the specific instances go out of scope.

Related

Is there a mechanism for a register bit value to control some other attribute?

Looking for something to change a mode or parameter based on value of register bit(s). Something like an 'on_modify' for a bit collection. Does that already exist?
I don't think so, though the reverse (update a parameter and have it reflected in the register) is supported if that is any good to you.
See the last example about binding register bits to a live parameter here - http://origen-sdk.org/origen//guides/models/parameters/#Live_Updating_Parameters
Adding some new functionality to bit collection so that it supported something like the following API should not be too difficult:
my_reg.my_bits.on_data_changed do |data|
do_something.based_on(data)
end
The on_data_changed method would just store the given block in the bit collection instance and then trigger it whenever the write method is called.

Storing state in a Node/Express application

I'm writing a Node/Express app (my first) and need help persisting what feels like a global variable across page loads; but using a global variable feels wrong.
For the purposes of the question, the site is essentially 3 different table views on the same data. The data in the tables is taken from 3 separate MongoDB tables, merged together with a function, and displayed (using Pug, iterating over the object I just created).
The 3 different views are just different Pug templates iterating over the same object.
I have middleware which generates this object from MongoDB and stores it on res.locals, but I don't want to re-generate it by default every time the user selects a different view. The data hasn't changed, and this feels wasteful.
I want to be able to set some sort of variable, dataNeedsToBeUpdated, and if true then the function inside my middleware will actually do the work and regenerate the table; if false, it'll just skip the operation.
If the user performs one of my update operations, I'll set dataNeedsToBeUpdated = true, redirect, middleware will fire, and the data will refresh before the next page load.
How do I do this properly?
app.locals works just about like res.locals, just it's available to the whole app. You can access it in middleware from req.app.locals. The flow as you've described it sounds good; just use app.locals for storing the data and the dataNeedsToBeUpdated.

Transition to route's action from Ember object

I am fairly new to ember. I have an existing Ember App and i need to implement a functionality in it. I have a Ember Object as below
`import Ember from 'ember'`
CallService = Ember.Object.extend
... other code here
_updateConnectedLeadId: (->
console.log "Do i get here??"
**pass the route action here**
).observes('some_other_here')
`export default CallService`
Unfortunately, i couldn't put the whole code here.
My route looks like
ApplicationRoute = Ember.Route.extend
actions:
showLead: ->
console.log data
console.log "did i get here?"
#transitionTo('dashboard')
`export default ApplicationRoute`
I tried using #send('showLead'), #sendAction('showLead') in my method but no luck.
My main intention is to make a transition once the console.log "Do i get here??" is displayed. I am not sure if i am on the right way here.
I also tried using #transitionTo('dashboard') and #transitionToRote('dashboard') directly but it throws me errors.
I have been stuck for a day on this now and i am clueless.
I'll be grateful for any guidance and help. Thanks
You have the problem that you are trying to trigger a route action or trigger a transition from within an Ember.Object, named call service. The code you create is unclear about where your custom object is being created; where the observer is triggered due to a change to object's property update, and so on.
Nevertheless, I tried to provide a working example for you. If you open the twiddle, you will see that I created a my-object instance within index.js route and pass it as model to my-component. When you click the button within my-component.hbs. The my-object instances dummyVariable is toggled and the observer within my-object executes. The tricky part here is that I passed index route itself as ownerRoute property to my-object instance; so that I can trigger the index route's dummyAction from within my-object.js with
ownerRoute.send('dummyAction');
so that related action executes and transition to my-route is performed. Although, I believe this might solve your question; I am not quite happy about the design. I do not think, it is a good way for Ember.Objects to know about routes, actions, controllers, etc. I believe the proper way is observing object's relevant properties from within this constructs and perform necessary actions by their own. Moreover, you might consider creating a service instead of an object and inject the service directly to your route instead of creating an instance of your class extending Ember.Object. If you have to extend from Ember.Object you can just inject relevant route, controller, etc to the instances of that particular object class by using an instance-initializer if you need.
Anyway, please take a look at the twiddle and ask more if you need to. I will be happy to help if I can.

Changing global variable in Polymer

I've got a component called app-globals which for the moment only has javascript with a variable in it. When clicking a button I want to not only change the variable value, but bind that value to a number of places throughout the app.
I can't find any documentation for this.
Any help?
That question is already answered here:
Polymer global variables
In short, use objects to store globals:
"By using an object with data properties, as in the edited version
above, and only ever reading from and assigning to the data properties
of that object rather than overwriting the object itself, changed
values are shareable between instances."

Referring to session-based data in a Mongoose virtual attribute

I have a Mongoose model that holds Places. Each place has a lat/lng. I can define a Mongoose virtual attribute called distance that would be used to sort Places in ascending order. What's the best way to refer to the user's location information (let's assume it's stored in a session variable for now) from inside the distance virtual attribute?
For anything involving external data, adding a method to the schema would be a better choice than a virtual property.
I'm solving a similar issue. The problem is that methods are fine if you want perform an operation on a single value but I'm retrieving a list and want to inject a new virtual field into every record in the list - but use session data to generate the field. to do this safely (avoiding globals), I think I'll need to use a QueryStream and inject the new field using an ArrayFormatter that takes the session variables as constructor parameters.
This also looks like a job for LINQ so another approach might be to use one of the ports of LINQ to JS.
If you sill prefer to use virtuals, you can store user location info in NodeJs globals. For example this code may be set after user login:
global.user_location = user.location;

Resources