Where to logic? 
Tags: javascript, webdev
Published: 2018-12-05 01:00:15

---
title: Where to logic? 
published: true
description: Thinking about loading websites from javascript logic in the browser or in the model
tags: js, webdev
---

## Motivation
**me**: Did you know that you can return HTML from a method in a mongoose schema?
**also me** : I did not.
**me** : Sure. I just tried it out, now, you can use methods like item.getSummaryContainer() to get your summary container HTML.
**also me** : Cool._runs off to create a bunch of methods to return HTML, deletes if/then statements in views and replaces with method calls_


minutes pass 


**also me**: Wait, but why?
**me**: _blinks_

## Discussion
So many times as a developer, I've come up with some new (to me) idea and then jumped in with two feet only to later wonder if perhaps the idea was not so good. Actually deciding is hard and requires experience that not all of us have.

My current rendition of a new and fun, but perhaps not good (or maybe it is) idea is as good as any to drive the conversation about how developers should approach these "Wait, but why?" decisions. I don't personally have the answers, but I hope that discussion in the comments will help suss it out. 

### Setup
I gave a bit of it away in the motivation - what if we return HTML from mongoose schema methods. For this example, we have a Mongo Express Node stack. I'll show two code approaches to one very simple display if/then. Assume in both cases that user.things are returned as part of the response from the server. 

#### Logic in EJS
When the logic is in the EJS, the schema is just the variables (or other methods, but none shown here for simplicity), and the HTML is broken by if/then statements.

**thing.js**
```
const mongoose = require('mongoose');

const thingSchema = mongoose.Schema({
     name: { type: String },
     description: { type: String },
     cost: { type: Number }
 });

module.exports = mongoose.model('Thing', thingSchema);

```
**userthings.ejs**
```
<% include ./partials/header %>
<div class="container-fluid">
  <div class="col-12 col-md-8 mx-auto">
    <div id="upcoming">
      <h2>Things</h2>
      <div class="row">
        <% user.things.forEach(function(thing){ %> 
          <div><h1> <%= thing.name %> </h1>
            <% if (thing.sold) { %>
              <h5> was sold for <%= this.cost %> </h5>
            <% } else { %> 
              <h5><%= this.description %> for just <%= this.cost %></h5>
            <% } %>
            </div>
        <% }) %>
      </div>
  </div>
</div>
<% include ./partials/header %>
```

#### Logic in Mongoose Schema
When the logic is in the schema, the HTML is not clear from the EJS page, but the page is clean.
 
**thing.js**
```
const mongoose = require('mongoose');

const thingSchema = mongoose.Schema({
     name: { type: String },
     description: { type: String },
     cost: { type: Number },
     sold: { type: Boolean )}
 });

thingSchema.method({
    printinfo: function () {
      let theinfo = '<div><h1>'+this.name+'</h1><h5>'+this.description+' for just '+this.cost+'</h5></div>'
      if (this.sold) {
        theinfo = '<div><h1>'+this.name+'</h1><h5> was sold for '+this.cost+'</h5></div>'
      }
      return the_info
    }
});

module.exports = mongoose.model('Thing', thingSchema);
```
**userthings.ejs**
```
<% include ./partials/header %>
<div class="container-fluid">
  <div class="col-12 col-md-8 mx-auto">
    <div id="upcoming">
      <h2>Things</h2>
      <div class="row">
        <% user.things.forEach(function(thing){ thing.printinfo(); }) %>
      </div>
  </div>
</div>
<% include ./partials/header %>
```

Ultimately, both return the same HTML and present the same results. Which is better? Is one more correct? Why? I think it is possible that the answer may be "just do what feels right for you" but I don't actually *know*. I'd like to know how folks work this kind of stuff out. 

## Game Plan
My game plan at this point is to continue to try, fail, and try again with things. I will continue to read to learn about others' ideas and to share when new things come along to excite or surprise me. I love that the dev community can be so supportive and creative in this wild and free land of the interwebs. 

**I hope that you, fellow devs, will put your ideas on how devs, especially less experienced devs, can make choices like this, below.**