I am wondering if something as basic as client/server operation model is easily doable with Meteor (meteor.com) framework.
A primitive template should
<template name="input">
<div>
<input type="text" val="">
<input type="submit">
</div>
</template>
<template name="output">
<div id="output">
</div>
</template>
wait for input, calls a server to execute serverFunction() on the input's value, and insert the result into the output tag. No collections, mongo, or authentication required. Of course, every client is supposed to receive its own results. It seems that Meteor.publish() operates only on collections.
Have a look at the Methods section in Meteor documentation:
http://docs.meteor.com/#methods_header
"Methods are remote functions that Meteor clients can invoke."
There's also code in the Wordplay example to show how to work this RPC mechanism (see the definition of Meteor.methods({ ... }) in model.js and game.js in this example project for more info).
a Meteor Collection is only associated with a Mongo collection if you tell it to be.. you can also use them to wrap arbitrary data.
The three main benefits of Meteor Collections (for me, at least):
the publish / subscribe model for keeping server and client data in sync
they can wrap persisted (Mongo) or arbitrary data
the ability to query them with Mongo-like syntax
This is what I was looking for:
server:
Meteor.methods =
doStuff: (input) ->
serverFunction input
client:
Template.input.events =
'submit': ->
Meteor.call 'doStuff', $('input[type=text]').val(), (error, result) -›
Session.set 'result', result
Template.output.output = ->
Session.get 'result
Related
I'm working with polymer and I'm testing all in my local environment and all is working as expected, but the problem is when I'll move it into production, where the url shouldn't be the same as in my local. In node.js I can set it via the config.json file and catch it with app.get('config') or something like that, but in polymer I cant get it, and the only chance I have is to create another endpoint with all the config.json config file, but, and here is the best part, I should hardcode this url in polymer!! (so annoying). There is some way or library or component or something that I'm missing ?
Thanks in advance guys! StackOverflow and the users helped my a lot!
UPDATE
The thing is that I'm using custom elements (because I had handlebars too and there is a little problem with the {{}}) so, in the custom elements that I created I have this (for example in the core-ajax call):
<core-ajax id="login" auto="false" method="POST" contentType="application/json" url="/app/middle/login" ....></core-ajax>
as you can see the url is a little bit hardcoded, I want to use something like this (this is what I have on the node.js endpoint application):
router.post(endpoint.login, function (req, res) {
and I want to got something like that over polymer
<core-ajax id="login" auto="false" method="POST" contentType="application/json" url="{{login}}" ... ></core-ajax>
<script>
Polymer('log-form', {
login: endpoint.login,
ready: function () {
....
})
});
</script>
I'ts more clear now? I'm not so strong on english language.
Thanks Guys, for all!
I am new to nodejs. I am basically using express, and I have to create a form to upload photos. So far I have created a file called upload.handlebars where I have written the following code:
<form enctype="multipart/form-data" action="uploads" method="post">
<input type="file", id = "image", name="image", accept="image/*">
<input type='submit' id='tags' value='Upload'>
I have another file called router.js where I have written the post function. One of the lines in the post function is:
fs.readFile(req.files.image.path, function (err, data) {
However, I get an error as follows:
TypeError: Cannot read property 'image' of undefined at Object.handle....
How should I specify the 'name' of the image file in my router.js?
Make sure you have multiparty middleware enabled.
https://github.com/andrewrk/connect-multiparty
Latest express doesn't come with it.
you can use express to solve this problem quickly. to know more read the folowing page express api reference
for now the following code may help you -
app.post('<your path>',express.bodyParser(),function(req,res){
console.log(req.files);//to get all the files uplaoded
});
make sure express version should 3.4
Multiple files are being posted to nodejs application using Express and bodyParser.
The number of files change in every request. It is required to get number of files posted at the server.
I have tried using
req.files.length
but it gives undefined. How can I know the number of files posted in a post request?
Also how to loop through each file?
Putting answer my self.
It should be iterated upon like this
for (x in files){
//code for handling each object in json.
}
You can get the length of and iterate a collection of files with the name of the <input>:
// example: <input type="file" name="images" multiple>
req.files.images.length;
req.files.images.forEach(function (file, i) {
// ...
});
You can find an example of this in the Connect repository: examples/upload.js
I'm creating a simple Hack2 app, and I can read body data with:
directory :: Application
directory env = do
body <- input_bytestring env
...
I'm trying to switch my form to use file uploads
<form action="/directory" method="POST" enctype='multipart/form-data'>
<div><input type="file" name="data"></div>
<div><input type="submit"></div>
</form>
But it's giving me a ShortWriteException. Maybe input_bytestring can't handle multipart. Is there a library that can handle multipart form data? Any examples of doing this with Hack2?
I never did figure this out. I switched to Happstack-lite, because I couldn't figure this out, and it seems like nobody is using Hack2.
I am in a bit of trouble as I am not able to find resources and/or tutorials that give me enough knowledge how to do this properly:
I am building a Couchapp uppon a contact database. For this I need to have a unordered list of the contacts(only the names) on the landing page. After examining this now for quite a time and examining the http://kansojs.org framework, I think I might have to ask here at Stackoverflow how this is done properly...
Here is what I ended up with (not working):
I started to setup a view (file 'views/contactslist/map.js ):
function(doc) {
if (doc.displayName) {
emit(doc.displayName, {displayname: doc.displayName});
}
};
... which basically gives me back this response:
{"total_rows":606,"offset":0,"rows":[
{{"id":"478d86edbbd94bbe627f3ebda309db7c","key":"Al Yankovic","value":{"displayname":"Al Yankovic"}},
{"id":"478d86edbbd94bbe627f3ebda30bb5cb","key":"Al-Qaeda","value":{"displayname":"Al-Qaeda"}}
]}
Afterwards, I created a new directory in the evently directory, 'contacts' and created the files "mustache.html", "data.js" and "query.json":
mustache.html:
<ul>
{{#contacts}}
<li>
<div class="name">
{{displayname}}
</div>
<div style="clear:left;"></div>
</li>
{{/contacts}}
</ul>
data.js:
function(data) {
$.log(data)
var p;
return {contacts : data.rows};
};
query.json:
{
"view" : "contactslist",
"descending" : "true"
}
Then I added
and
$("#contacts").evently("contacts", app);
to the index.html in the _attachments directory.
Watching the console in Firebug I can not see any Request/Response from CouchDB returning my vie's results, so I think it is not even requested. Where did I take the wrong turn?
data.js, query.json and mustache.html need to be in evently/contacts/_init/
_init means that this gets executed on widget initialization.
Going over this Tutorial helped a lot.