These are chat archives for mithriljs/mithril.js

14th
Apr 2016
aucelum
@aucelum
Apr 14 2016 00:35
@patrkris Yep. I've been using mithril + closure but not the externs route
khades
@khades
Apr 14 2016 02:45
i sill dont get redux
i get idea of reducers as state morphers
khades
@khades
Apr 14 2016 04:41
i need nodejs slight help
James Forbes
@JAForbes
Apr 14 2016 05:44
hey @lhorie that was a great explanation. Seems like its a lot like Elm, except the action types are strongly typed in Elm. Also a lot like: https://github.com/paldepind/functional-frontend-architecture
@khades I can help w/ node probably
khades
@khades
Apr 14 2016 05:44
ok, i got mongodb driver
that returns db object in a promise body
it is done for connection pooling
i am REALLY tired of passing "db" in every function and object
1) is it possible to make module that will return me that exact "db" object?
James Forbes
@JAForbes
Apr 14 2016 05:45
yep
khades
@khades
Apr 14 2016 05:45
2) is it ok otherwise pass db into global?
James Forbes
@JAForbes
Apr 14 2016 05:45
either is fine, but I think the first idea is better
depends on the type of application though
khades
@khades
Apr 14 2016 05:46
require and set, and then just require and get, yea?
i have no idea about "caching" in module system
there's no need db object in routes
James Forbes
@JAForbes
Apr 14 2016 05:47
when you require in node, it only ever executes the module once
khades
@khades
Apr 14 2016 05:47
i see
so it creates object and then works with one instance
James Forbes
@JAForbes
Apr 14 2016 05:49
how many methods on the instance do you use? I've not used mongo much. It's mostly collection and find isn't it?
khades
@khades
Apr 14 2016 05:49
yes, collection function
find, update
James Forbes
@JAForbes
Apr 14 2016 05:50
ah great
khades
@khades
Apr 14 2016 05:50
but every repository i have has its own collection
named one
collection("users")
khades
@khades
Apr 14 2016 05:50
i did read this
Evgeniy Labutin
@LabEG
Apr 14 2016 05:51
@JAForbes i'm not see architecture by link =D
khades
@khades
Apr 14 2016 05:51
Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee that require('foo') will always return the exact same object, if it would resolve to different files.
that's the most concerning for me
James Forbes
@JAForbes
Apr 14 2016 05:51
@khades where are you storing your connection details? Is it hard coded, or is there an .env file or something else?
khades
@khades
Apr 14 2016 05:51
any way i want it to be
James Forbes
@JAForbes
Apr 14 2016 05:51
@khades that is only true for module's you've installed from npm
khades
@khades
Apr 14 2016 05:52
right now - hardcoded
This message was deleted
MongoClient.connect('mongodb://localhost:27017/vote', function(err, db) {
//there goes app initalisation
})
also for what wrecked reason
James Forbes
@JAForbes
Apr 14 2016 05:53
@LabEG strange that link seems to work for me
khades
@khades
Apr 14 2016 05:54
the whole lib has ERR first and result second
James Forbes
@JAForbes
Apr 14 2016 05:54
Ok cool that's a nodejs convention, and very helpful
Because we can easily promisify it
Evgeniy Labutin
@LabEG
Apr 14 2016 05:54
@JAForbes link work, but i'm not see architecture in samples =D
khades
@khades
Apr 14 2016 05:54
this is a promise
i just used non-promise notation
but the promises are messed up as welll
James Forbes
@JAForbes
Apr 14 2016 05:55
So connect returns a promise already?
khades
@khades
Apr 14 2016 05:55
one param promise returns ERROR
@JAForbes yes
changed to
MongoClient.connect('mongodb://localhost:27017/vote').then(function(err, db) {
James Forbes
@JAForbes
Apr 14 2016 06:09

OK great. So the idea here is, the connect function will be called by your collection fn whenever you invoke it. If there is no connection, it will set it up, if there is, it just resolves it.

As a user of the module, you don't have to invoke connect yourself. You just invoke collection

//usage

var db = require('./db') 

db.query('animals', {  type: 'giraffe'}, function(err, animal){
   // do stuff here
})

You could also just resolve the connect and use the traditional API:

var db = require('./db')

db.connect().then(function(){
   var cursor = db.collection('animals').find({ type: 'giraffe' })

  cursor.each(function(err, animal){

  })
})

You'd probably want to play with how you define the functions. This is purely just a sketch.
You may want to make each record an individual promise, and return Promise.all, so you can interact with the records in a more general manner. It's completely up to you.

Here is a gist of how that module could look.

var DB = {
  connect: function(){
     if(!DB.connection){
           DB.connection = MongoClient.connect('mongodb://localhost:27017/vote')
     } else {
          return Promise.resolve(DB.connection)
     }
  },
  query: function(collection, where, callback){
      return DB.connect()
      .then(function(db){
           return db.collection(collection).find(finder).each(callback)
      })
  },
  close: function(){
      if(DB.connection){
         return DB.connection.close()
      } else {
        return Promise.resolve()
      }
  }
}


module.exports = DB
khades
@khades
Apr 14 2016 06:10
no no no no
no
you're forgot about Pool management
if you connect on every query you get NO connection pooling
James Forbes
@JAForbes
Apr 14 2016 06:11
it's not connecting every query, read what I said, and read the code
khades
@khades
Apr 14 2016 06:11
  return DB.connect()
  .then(function(db){
       return db.collection(collection).find(finder).each(callback)
  })
also whole this line is mess for my purposes
return db.collection(collection).find(finder).each(callback)
also my question was about db object
returned by connect function
and nothing else
James Forbes
@JAForbes
Apr 14 2016 06:14
@khades no offence I think you are not really understanding the code I wrote
DB.connect returns the db object, which is the answer to the your question. It also only will connect once. And I also said, this is a sketch, you'd need to play with the implementation
khades
@khades
Apr 14 2016 06:15
ye, that code restricts all queries to (find)
also .each on cursor is not a good idea
this code is generally ok
James Forbes
@JAForbes
Apr 14 2016 06:15
haha @khades I can't tell if you are serious
khades
@khades
Apr 14 2016 06:15
but as mongo client it is not good
James Forbes
@JAForbes
Apr 14 2016 06:16
This isn't a real thing I'm deploying to npm, this is an illustration of how to make the db instance available from any file
which was the question you asked
you are meant to write the real code
khades
@khades
Apr 14 2016 06:16
n
no you wrote not what i asked for
you return me wrapped function
without access to db
you even narrowed query type to find
and if i need a "query"
or 'remove"
i will need to extend this client
generally idea is ok
but having empty DB or db connection with error
will get a lot of sanitation after
James Forbes
@JAForbes
Apr 14 2016 06:18
all your issues are already handled in the above code, it just requires knowledge of promises
khades
@khades
Apr 14 2016 06:18
i know what promise is
James Forbes
@JAForbes
Apr 14 2016 06:19
so what does db.connect resolve to
and what is db.connection
i mean db.connection
db.connect will return either new connection promise, or reuse old one
James Forbes
@JAForbes
Apr 14 2016 06:20
So it resolves to the db object, which is what you wanted. It also handles connection errors, via catch on the promise chain
khades
@khades
Apr 14 2016 06:20
this is not a db object
i wanted
also
 .then(function(db){
       return db.collection(collection).find(finder).each(callback)
  })
this will use FIRST argument of promise
WHICH IS ERROR
which i said earlier
James Forbes
@JAForbes
Apr 14 2016 06:21
Earlier when you mentioned err being the first argument. That is not a promise
that is a callback
there are two different ways to talk to the database, either a traditional nodejs callback, or a promise. In the promise, the first (and only argument) will be database. The error would go to a catch function if you chose to implement it.
khades
@khades
Apr 14 2016 06:22
again
connectCallback(error, db)
Stephan Hoyer
@StephanHoyer
Apr 14 2016 06:23
Could you make this offtopic conversation private plz?
James Forbes
@JAForbes
Apr 14 2016 06:23
yes sorry @StephanHoyer
I thought it was going to be quick
Stephan Hoyer
@StephanHoyer
Apr 14 2016 06:24
thanks!
James Forbes
@JAForbes
Apr 14 2016 06:24
:)
Evgeniy Labutin
@LabEG
Apr 14 2016 07:58
@JAForbes this is not architecture, this is trash =D
cyberco
@cyberco
Apr 14 2016 12:50
Any tips on testing Mithril apps? Where to start...
btw, the link to the mock object is broken on http://mithril.js.org/mithril.deps.html
that should probably be fixed
Justin
@WreckedAvent
Apr 14 2016 13:33
You can also just call and inspect mithril components/views manually, since the output of m is just a javascript object
Stephan Hoyer
@StephanHoyer
Apr 14 2016 13:34
@WreckedAvent mithril-query basically does this
just more conveniently
Justin
@WreckedAvent
Apr 14 2016 13:34
var actual = myComponent.view({ dummy: "controller data" }, { dummy: "prop args" })
etc
@StephanHoyer I hadn't looked at it, but I've always just tested mithril just by calling it in a manner like that
var output = simple.view(simple.controller());
$output = mq(output);
yeah, I see
Stephan Hoyer
@StephanHoyer
Apr 14 2016 13:36
you should defenitly check out mithril query :D
Justin
@WreckedAvent
Apr 14 2016 13:40
That's not shameless self promotion, is it? :wink2:
Stephan Hoyer
@StephanHoyer
Apr 14 2016 13:41
no. I just want to make your developer life easier :D
I once started writing tests this way. For controllers it's ok, for views it's really cumbersome.
Nowadays I only rarely write controller tests but test the component as a whole
Justin
@WreckedAvent
Apr 14 2016 13:44
I usually don't have much/any controller logic, most components just use their props passed in.
Stephan Hoyer
@StephanHoyer
Apr 14 2016 13:44
:thumbsup:
Justin
@WreckedAvent
Apr 14 2016 13:44
in fact a lot of my components look like
import m from "mithril";

export view(_, { onClick }) => { ... }
happily everything related to the controller is optional
eguneys
@eguneys
Apr 14 2016 15:39
no I add a tr into a table tbody already!
Leo Horie
@lhorie
Apr 14 2016 15:41
can you make a jsfiddle with a test case?
eguneys
@eguneys
Apr 14 2016 15:55
thank you I removed the key and it is fixed, I will make a jsfiddle to show
Leo Horie
@lhorie
Apr 14 2016 16:10
thanks
Barney Carroll
@barneycarroll
Apr 14 2016 18:09
we need a big fat TODOs somewhere with 'in-depth keys documentation including gotchas' as the first item
Stanislav Termosa
@termosa
Apr 14 2016 18:30
Hi!
Can anyone suggest me alternative links to https://lhorie.github.io/mithril/auto-redrawing.html ? I need more information on Mithril flow and I can't find it :(
Leo Horie
@lhorie
Apr 14 2016 18:31
@termosa what sort of thing are you looking for?
Stanislav Termosa
@termosa
Apr 14 2016 18:39
After reading auto-redrawing topic it's still not clear for me when updates take place. I wrote Hello world app and I'm trying to print value of Input in my view at the same time it's typed in field
Let me show you code in sandbox, give me several minutes for that
Pat Cavit
@tivac
Apr 14 2016 18:46
It's not redrawing while you typed because you used the onchange event
which does not fire until the field is blurred
oninput will fire while the <input> is being modified
Stanislav Termosa
@termosa
Apr 14 2016 18:46
oh
my bad
Pat Cavit
@tivac
Apr 14 2016 18:46
if you type into your field, then click off the change event fires and you can see mithril redraw correctly
Stanislav Termosa
@termosa
Apr 14 2016 18:47
Please, correct me if I'm wrong:
Mithril redraw app if it handles event mentioned in node properties
Am I right?
Pat Cavit
@tivac
Apr 14 2016 18:48
yes
Stanislav Termosa
@termosa
Apr 14 2016 18:48
Or it redraw the app on updating one of m.prop() ??
Pat Cavit
@tivac
Apr 14 2016 18:48
m.prop is just a tiny convenience function
Stanislav Termosa
@termosa
Apr 14 2016 18:48
@tivac got it!
that's nice
Thanks a lot
Pat Cavit
@tivac
Apr 14 2016 18:48
off the top of my head mithril only redraws on DOM events, route change, and calls to m.redraw()
Stanislav Termosa
@termosa
Apr 14 2016 18:52
Thanks again, that is helpful
Leo Horie
@lhorie
Apr 14 2016 19:03
@termosa @tivac and on calls to m.endComputation()(assuming there was a corresponding m.startComputation()
Stephan Hoyer
@StephanHoyer
Apr 14 2016 19:10
and therefor on ajax responses if you use m.request
Pat Cavit
@tivac
Apr 14 2016 19:29
oh sure, I've never used m.request w/o also setting background: true so I forget about it XD
Dominik Dumaine
@Bondifrench
Apr 14 2016 21:52
@nallerooth I noticed you tweeted on Mithril.js at #Mithril, it's better to use the #MithrilJs channel as the first one is more LOTR related stuff
Pat Cavit
@tivac
Apr 14 2016 21:58
haha