Question

I have a data model as follows in jaydata

Model.js

$data.Entity.extend('Expense', {
  Id: { type: 'int', key: true, computed: true },
  Name: { type: 'string' },
  Date: { type: Date },
  Price: { type: 'number' }  
});

$data.Entity.extend('Regular', {
  Id: { type: 'int', key: true, computed: true },
  Name: { type: 'string' },
  Price: { type: 'number' }  
});

$data.EntityContext.extend("ExpenseDatabase", {
    Expenses: { type: $data.EntitySet, elementType: Expense },
    Regulars: { type: $data.EntitySet, elementType: Regular }
});

I want to filer the results by Date, which i havent been able to. Can someone help me with this regard? My code for filtering is as follows. But its not working, throws up a type error.

var d = new Date();
exDB.Expenses
    .filter( function(exp) {            
        return exp.Date.day() == d.getDate();                    
    })
    .forEach( function(exp) {
        alert(exp.Name + exp.Date.getDate());
        toAdd = '<li><a>'+exp.Name+'<span class="ui-li-count">'+exp.Price+'</span></a></li>';
        $("#expenseList").append(toAdd);
        $("#expenseList").listview("refresh");
    });
Was it helpful?

Solution

The generic solution would be to write a query with two conditions that checks for an interval:

//first second of the current day
var intervalStart = new Date(new Date(date - date % (24 * 60 * 60 * 1000)));
//last second of the current day
var intervalEnd = new Date(intevalStart .valueOf() + 24 * 60 * 60 * 1000);

exDB.Expenses
    .filter( function(exp) {            
        return exp.Date >= this.minDate && exp.Date <= this.maxDate
    }, {minDate: intervalStart , maxDate: intervalEnd })
    .foreach(function(){...});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top