In a Meteor template with Blaze I want to simply duplicate a piece of text entered by the user onto another part of the screen. Is there a way to do this without writing helper JS code? Something like:
<input value="{{theValue}}" type="text">
<p>{{theValue}}</p>
I don't want to store the data in a collection, just duplicate it. Seems like this can't be done without writing a JS helper.
Like you said,i am unaware of any method too without writing a helper for this,
if you are taking the user's input and then you want to display it elsewhere,it would be best to store the input in a Session or reactive variable and then later display it with a helper
in the input event
Session.set("inputValue",the-user's value);
and return a helper wherever you want to display it
'userValue' : function(){
return Session.get("inputValue");
anyway i would update the answer if i find something that can be done without using a helper
Related
I'm designing an ecommerce website using Node, Express and Pug. When a customer adds an item to their basket, I'd like to display a count of the items next to the basket icon which appears on every page. Like how I can say app.use(some_middleware_func) and have it apply to all routes, can I define a variable to be passed to all res.render('template', { data: some_data }) statements? The only way I can see of doing it is to manually include the information in the data object for each statement but that doesn't seem very maintainable.
Hopefully somebody has an idea or perhaps there is a much better way to approach this?
I'm using flask-sqlalchemy on a site to save some information from a textarea. This text area could contain some simple bbcode that I would handle later to allow text formatting.
When I save or update anything, everything looks normal before and after the update operation. When I go to edit that same information, it has <br>, <strong> and other tags that have been placed in the text. Is this something that flask-sqlalchemy does? If so, how do I disable this? It seems like the problem is in flask-sqlchemy based on where the problem happens, and I'd rather handle any formatting myself.
I've also checked how the data is stored in the database. The database contains the HTML code rather than the bbcode markup, so the problem does not lie in the templates that display the editing view.
EXAMPLE
In the form, I submit this data.
[b]hello world[/b]
how are you
In the flask route for the update, I print out the data
#app.route("/update/item/<id>", methods=['POST']
def update(id):
# While testing I'm not going to bother with escaping anything.
# Once I figure out what's causing this, I'll do so with the escape() function.
item = request.form['item']
print(repr(item)) # "[b]Hello World[/b]\r\n\r\nhow are you"
Item.query.filter_by(id=id).update(dict(item=str(item))
queried = Item.query.filter_by(id=id).first()
print(queried.item) # "[b]Hello World[/b]\r\n\r\nhow are you"
When i go to edit the item, it displays this in the text area
<strong>Hello World</strong>
<br>
<br>how are you
rather than
[b]Hello World[/b]
how are you
UPDATE:
This only appears to happen when the following code is ran
#...
def paginate(page):
items = Item.query.order_by(Item.id.desc()).paginate(page=page, per_page=10)
for text in items:
# Just replacing [b] and [/b] as an example here. I'll have everything
# regexed out and replaced in a dedicated function.
# This replacing appears to be the culprit. Commenting this out fixes it.
text.item = text.item.replace("[b]", "\<strong\>")
text.item = text.item.replace("[/b]", "\</strong\>")
return items
This problem was brought about by not knowing that sqlalchemy automatically saves changes back to the database when an attribute is changed, for example, by parsing the bbcode for later use. While I may have strong opinions on mutable data structures automatically saving themselves (it should NEVER happen), here's how to fix this if anyone else has the same problem.
First, know that sqlalchemy will automatically save if you modify anything on your sql query unless you detach it from the database, EVEN IF you use autoflush=False and autocommit=False (I tried both). Before making any changes, call the expunge() function and then make your changes. This detaches the query from the database and allows you to modify to your heart's content without any (potentially devastating) data mutation happening in your database.
In my case, this means my paginate function should look like this
# Make sure you have your db object imported somewhere above
def paginate(page):
items = Item.query.order_by(Item.id.desc()).paginate(page=page, per_page=10)
for text in items:
db.session.expunge(text)
text.item = text.item.replace("[b]", "<strong>")
text.item = text.item.replace("[/b]", "</strong>")
return items
I wonder if there is a way to write some show, list or update functions in a design document as I can do for a view one in Fauxton (with its editor).
When I create a view in Fauxton, I simply click the "Add view..." button. Then, I fill the fields for the design document and view names, and I can write my JS Code directly in the editor. When I save, the design document is correctly generated with my view function correctly escaped.
When I want to write a list for example, I have to edit the design document and write my function, for example:
...
"lists": {
"my-list": "function(head,req) {\n send(\"Simple Test\");\n}"
},
...
But it's tricky to correctly insert \n or \" as the function has to be passed as a string. It's very easy to forget something writing a function in this way.
I can't find a "add list..." option somewhere in the interface, so I use to create a new view with the application editor, change the function signature, save the document and then edit it again to cut/paste the function in its correct place, but it's not a solution.
So, what could be a better way to write theses functions ? (even with an external editor and then upload the function ?) Is there a way to write the JS Code in a external editor (Atom for example) and then "generate" the correct espaced string value for this function to upload into couchdb via curl?
Consider using Photon https://github.com/ermouth/couch-photon, it has decent editor for JS functions in JSON docs.
I'm building an app in Express. In one of the views a logged in Superadmin is able to view all available clients/user. In this view I am loading a bunch of client data from my MongoDB/Mongoose with a simple:
app.get('/superadmin', function(req, res) {
Client.find({}, '_id company monthly_cost sms_cost', function (err, docs) ...
As you can see above i have choosen only the values that I need from the query. These four are: "_id", "company", "monthly_cost" and "sms_cost"
From the "_id" i can get a "creation date" by using .getTimestamp(), but the Dateobject this function return is bit to complex formated. I need a simpler date, something like: (YYYY-MM-DD). Im thinking of using a small node plugin like dateformat or possibly writing a very simple function that extract the YYYY, MM and DD from the IsoDate object and saving this in a new variable/array
Now to my questions:
Q1) WHERE is actually the right place for this code? I'm currently putting it inside the route handler above... consequently it will follow right below the code above. I'm thinking this is principally the right way according to a MVC pattern. I'm thinking I dont want to put this code in the Jade view template?
Q2) IN WHAT FORM should i save this data and HOW should i pass it along to Jade. Should I somehow add it to the "docs"-data... that is, the data I extract from my DB. Or should I rather put this creationDate in a separate array which i pass to jade side by side with the original DB-data.
I hope my questions are clear enough!
Q1:
If your Mongoose-query is solely dependent on your route /superadmin, this is exactly the right place to put your code. If you are using the same snippet in different routes or functions you might as well wrap it in a function that is accessible to every route in question. But donĀ“t forget to also wrap req, res and other required variables. Have your calculations within your callback and use Jade only for representation of data.
Q2:
What do you mean by "save"? When you are already iterating over every document to do your calculations and transformations, create an extra field creationDate for every document and pass docs as a single parameter to the Jade file afterwards.
I am writing a simple crawler using VBA. I found out that the data I am looking for correspond to the node <h6 class="country-name" id="Australia">.
I know that if I wanted to select data from, let us say, <div class="section-country">, I should use .getElementsByClassName("section-country") in my VBA macro.
In presence of both class and id in the node, which command should I insert in my VBA macro to scrape data?
Thanks a lot,
Avitus
EDIT: writing .getElementsByClassName("country-name").getElementsById("Australia") gives me an error. Why?
getElementsByID (plural) doesn't exist - there should only be one item with a given ID. Therefore, use getElementByID (singular) which does exist. If there happen to be multiple elements with the same ID, this function will return the first one.
As others have said, selecting by ID sounds more appropriate to what you want to do than selecting by class
There must be a method like getelementbyxpath you can use this method by using this xpath "//div[#class='country-name' and #id ='Australia']"
eg: getElementsByXpath("//div[#class='country-name' and #id ='Australia']")
Read More here how to set up a crawler for web scraping