简体   繁体   中英

DS.FixtureAdapter - How to fix adapter without data error in Ember Data 1.0 beta 2

I'm using a DS.FixutreAdapter with stub data while I don't have access to the back-end. I've started upgrading Ember Data 0.13 to Ember Data 1.0 beta 2, but I keep getting an error:

"You made a request for a [user] with id [userid], but the adapter's response did not have any data"

A few moments ago it (seemingly randomly) started working when I moved the fixture definition...

App.User.FIXTURES = [{
  id: 'muosuser',
  firstName: 'John',
  lastName: 'Smith'
}]

...from the bottom to the top of my stub-data.js file, but that may have just been a fluke.

Any thoughts? The docs/guides haven't been updated yet for Data 1.0 beta 2, and everyone seems to ignore the FixtureAdapter when writing up transition guides, etc, so I'm kind of lost in the dark. For all I know, the old way of doing fixtures may no longer even apply!

Edit: Per request, here's where I'm trying to load the user:

App.MainRoute = App.AuthenticatedRoute.extend({

  redirect: function() {
    this.transitionTo('planner')
  }

, model: function() {
    return Ember.Object.create({
      user: this.store.find('user', this.controllerFor('login').get('username'))
    })
  }

, actions: {
    logout: function() {
      this.controllerFor('login').set('token', null)
      this.transitionTo('login')
    }
  }

})

Also per request, here's the entirety of stub-data.js :

//
// Setup
//

App.Store.reopen({
  adapter: 'fixture'
})

//
// Users
//

App.User.FIXTURES = [{
  id: 'muosuser'
, firstName: 'John'
, lastName: 'Smith'
}]

//
// Snapshots
//

App.Snapshot.FIXTURES = [{
  id: Ember.generateGuid()
, regions: []
, networks: []
}]

//
// Networks
//

var networks = []
var ridicNet = ["Network ", "Krowten "]
for(var i = 0; i < 12; i++) {
  var newId = Ember.generateGuid()
  networks.push({
    id: newId
  , name: ridicNet[i%2] + (i+1)
  , terminals: []
  })
  App.Snapshot.FIXTURES[0].regions.push(newId)
}
networks.push({
  id: Ember.generateGuid()
, name: "k"
, priority: "p4"
, voiceRecognitionRequired: true
, terminals: []
})
App.Snapshot.FIXTURES[0].regions.push(networks[networks.length - 1].id)
App.Network.FIXTURES = networks

//
// Regions
//

App.Region.FIXTURES = [{
  // id: Ember.generateGuid()
  name: "Alaska"
, minLat: 53
, maxLat: 71
, minLon: -167
, maxLon: -131
}, {
  // id: Ember.generateGuid()
  name: "Brazil"
, minLat: -33
, maxLat: 5
, minLon: -74
, maxLon: -35
}, {
  // id: Ember.generateGuid()
  name: "Caribbean"
, minLat: 14
, maxLat: 23
, minLon: -85
, maxLon: -58
}, {
  // id: Ember.generateGuid()
  name: "Central Africa"
, minLat: -5
, maxLat: 15
, minLon: 5
, maxLon: 40
}, {
  // id: Ember.generateGuid()
  name: "Chile"
, minLat: -58
, maxLat: -18
, minLon: -76
, maxLon: -64
}, {
  // id: Ember.generateGuid()
  name: "China"
, minLat: 20
, maxLat: 53
, minLon: 74
, maxLon: 134.5
}, {
  // id: Ember.generateGuid()
  name: "Columbia"
, minLat: -5
, maxLat: 13
, minLon: -80
, maxLon: -67
}, {
  // id: Ember.generateGuid()
  name: "East Africa"
, minLat: 15
, maxLat: 33
, minLon: 15
, maxLon: 38
}, {
  // id: Ember.generateGuid()
  name: "Hawaii"
, minLat: 18
, maxLat: 29
, minLon: -180
, maxLon: -154
}, {
  // id: Ember.generateGuid()
  name: "Australia"
, minLat: -40
, maxLat: -11
, minLon: 113
, maxLon: 154
}]
App.Region.FIXTURES.map(function(fixture) {
  fixture.id = Ember.generateGuid()
  App.Snapshot.FIXTURES[0].regions.push(fixture.id)
})

//
// Terminals
//

var terminals = []
  // , terminalResults = []
for(var i = 0; i < networks.length * 2; i++) {
  var network = networks[Math.floor(i / 2)]
    , region = App.Region.FIXTURES[Math.floor(Math.random() * App.Region.FIXTURES.length)]
    , terminalId = Ember.generateGuid()
    // , latLngId = Ember.generateGuid()

  terminals.push({
    id: terminalId
  , name: 'Terminal ' + (i+1)
  , network: networks[Math.floor(i / 2)].id
  , location: region.id
  // , latLng: latLngId
  })
  if (!network.terminals)
    network.terminals = []
  network.terminals.push(terminals[i].id)
  if (!region.terminals)
    region.terminals = []
  region.terminals.push(terminals[i].id)

  // var minLat = Math.min(region.minLat, region.maxLat)
  //   , maxLat = Math.max(region.minLat, region.maxLat)
  //   , minLon = Math.min(region.minLon, region.maxLon)
  //   , maxLon = Math.max(region.minLon, region.maxLon)
  //   , lat = Math.random() * (maxLat - minLat) + minLat
  //   , lng = Math.random() * (maxLon - minLon) + minLon
  // terminalResults.push({
  //   id: latLngId
  // , terminal: terminalId
  // , lat: lat
  // , lng: lng
  // })
}
App.Terminal.FIXTURES = terminals
// App.TerminalResults.FIXTURES = terminalResults

App.Stub = App.Stub || {}

// Computation
App.Stub.computeAll = function() {
  this._complete = false
  this.computeTerminals()
  this.computeSatellites()
  this._complete = true
}

App.Stub.computeTerminals = function() {
  // Retain IDs
  var terminalResultIds = App.TerminalResults.FIXTURES && App.TerminalResults.FIXTURES.mapProperty('id') || []

  // Clear previously existing data
  App.TerminalResults.FIXTURES = []
  App.Terminal.FIXTURES.map(function(terminal) {
    delete terminal.results
  })

  // Generate new data
  App.Terminal.FIXTURES.forEach(function(terminal, index) {
    if ( !terminal.location ) { return }
    var regions = App.Region.FIXTURES
      , region = regions.findProperty('id', terminal.location)
      , minLat = Math.min(region.minLat, region.maxLat)
      , maxLat = Math.max(region.minLat, region.maxLat)
      , minLon = Math.min(region.minLon, region.maxLon)
      , maxLon = Math.max(region.minLon, region.maxLon)
      , lat = Math.random() * (maxLat - minLat) + minLat
      , lng = Math.random() * (maxLon - minLon) + minLon
    //   , demand = Math.ceiling(Math.random() * 96)

    // switch(demand) {
    //   case 12:
    //   case 48:
    //   case 96:
    //   case 32:
    //   case 64:
    //   case 16:
    //     demand *= 100
    //     break
    //   default:
    //     demand = 0
    // }

    terminal.results = terminalResultIds[index] || Ember.generateGuid()
    App.TerminalResults.FIXTURES.push({
      id: terminal.results
    , terminal: terminal.id
    // , lat: lat
    // , lng: lng
    // , demand: demand
    , location: [lat, lng]
    })
  })
}

App.Stub.computeSatellites = function() {
  // Retain IDs
  var positionIds = App.SatellitePosition.FIXTURES && App.SatellitePosition.FIXTURES.mapProperty('id') || []
    , satelliteIds = App.Satellite.FIXTURES && App.Satellite.FIXTURES.mapProperty('id') || []
    , beamIds = App.SatelliteBeam.FIXTURES && App.SatelliteBeam.FIXTURES.mapProperty('id') || []
    , carrierIds = App.SatelliteCarrier.FIXTURES && App.SatelliteCarrier.FIXTURES.mapProperty('id') || []

  // Clear previously existing data
  App.SatellitePosition.FIXTURES = []
  App.Satellite.FIXTURES = []
  App.SatelliteBeam.FIXTURES = []
  App.SatelliteCarrier.FIXTURES = []

  var satellitePositionOptions = [[[0, -135], [0, -45], [0, 45], [0, 135]]]
  function generateContour(cLat, cLng, radius) {
    var ret = []
    for(var k = 0; k < 60; ++k) {
      var lat = cLat + radius * Math.sin(k * Math.PI / 30)
        , lng = cLng + radius * Math.cos(k * Math.PI / 30)
      ret.push([lat, lng])
    }
    return ret
  }

  // Generate new data
  // SatellitePositions
  for(var spk = 0; spk < 1; ++spk) {
    var positionId = positionIds[spk] || Ember.generateGuid()
    App.SatellitePosition.FIXTURES.push({
      id: positionId
    , satellites: []
    })
    // Satellites
    var totalSatellites = 4
    for(var sk = 0; sk < totalSatellites; ++sk) {
      var satelliteIndex = spk * totalSatellites + sk
        , satelliteId = satelliteIds[satelliteIndex] || Ember.generateGuid()
      App.SatellitePosition.FIXTURES[spk].satellites.push(satelliteId)
      App.Satellite.FIXTURES.push({
        id: satelliteId
      , location: [satellitePositionOptions[spk][sk][0], satellitePositionOptions[spk][sk][1]]
      , beams: []
      })
      // Beams
      var totalBeams = 6
        , distroRadius = Math.random() * 20 + 10  // minimum: 10, maximum: 30
      for(var sbk = 0; sbk < totalBeams; ++sbk) {
        var beamIndex = satelliteIndex * totalBeams + sbk
          , beamId = beamIds[beamIndex] || Ember.generateGuid()
          , contourRadius = Math.random() * (distroRadius / 2) + (distroRadius / 10)
          , contourCenter = {
              lat: App.Satellite.FIXTURES[satelliteIndex].location[0]
                  + distroRadius * Math.sin(sbk * 2 * Math.PI / totalBeams)
            , lng: App.Satellite.FIXTURES[satelliteIndex].location[1]
                  + distroRadius * Math.cos(sbk * 2 * Math.PI / totalBeams)
            }
        App.Satellite.FIXTURES[satelliteIndex].beams.push(beamId)
        App.SatelliteBeam.FIXTURES.push({
          id: beamId
        , contour: generateContour(contourCenter.lat, contourCenter.lng, contourRadius)
        , carriers: []
        })
        // Carriers
        var totalCarriers = 4
        for(var sck = 0; sck < totalCarriers; ++sck) {
          var carrierIndex = beamIndex * totalCarriers + sck
            , carrierId = carrierIds[carrierIndex] || Ember.generateGuid()
          App.SatelliteBeam.FIXTURES[beamIndex].carriers.push(carrierId)
          App.SatelliteCarrier.FIXTURES.push({
            id: carrierId
          , txPower: Math.random()
          , loadFactor: Math.random()
          , ovsf: Math.floor(Math.random() * 256)
          })
        }
      }
    }
  }
}

//Reloading
function wait(ms) {
  var deferred = $.Deferred()
  setTimeout(deferred.resolve, ms)
  return deferred.promise()
}

App.Stub.reloadAll = function() {

  this.reloadTerminals()
  this.reloadSatellites()
}

App.Stub._recordReload = function(record) {
  record.reload()
}

App.Stub.reloadTerminals = function() {
  App.Terminal.find().forEach(this._recordReload)
  App.TerminalResults.find().forEach(this._recordReload)
}

App.Stub.reloadSatellites = function() {
  App.SatellitePosition.find().forEach(this._recordReload)
  App.Satellite.find().forEach(this._recordReload)
  App.SatelliteCarrier.find().forEach(this._recordReload)
  App.SatelliteBeam.find().forEach(this._recordReload)
}

My guess is that there's some sort of error in your stud-data.js file that was preventing the User fixture from being set up correctly. Fixtures definitely still work.

Found it. I had overridden (and forgotten about) find on my adapter to hook it up with a 3rd party loading indicator. I called _super , but never returned anything. I don't know why it worked before, but I can definitely see why it wouldn't work now. Stupid mistakes are always the hardest to find!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM